473,888 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Microsoft Scripting Runtime #4

ADezii
8,834 Recognized Expert Expert
This is the last in a series of Tips involving the Microsoft Scripting Runtime Library and deals with creating, opening, writing to, reading from, and closing Text Files via this Library. At this time, the Scripting Library cannot deal with the opening and manipulation of Files in Binary Mode, so we will only demonstrate this functionality as it relates to Text Files. The code is fairly straightforward and sparsely commented, so I will not bore you with unnecessary details. The code is posted below, any questions, please feel free to ask them:
Expand|Select|Wrap|Line Numbers
  1. '******************************************************************
  2. Dim fso As FileSystemObject, ts As TextStream, fil As File, txtFile
  3. 'Set fso = CreateObject(“Scripting.FileSystemObject”)       OR
  4. Set fso = New FileSystemObject
  5. '******************************************************************
  6.  
  7. 'Opening a Text File and Reading its contents
  8. Set ts = fso.OpenTextFile("C:\Test\Test.TXT", ForReading, False, TristateUseDefault)
  9.  
  10. Debug.Print ts.ReadAll             'Reads entire File contents
  11. Debug.Print ts.Read(500)           'Reads 500 characters
  12. Debug.Print ts.ReadLine            'Reads 1 entire line
  13. Do Until ts.AtEndOfStream          'Reads entire File line by line
  14.   Debug.Print ts.ReadLine
  15. Loop
  16.  
  17. ts.Close
  18. Set ts = Nothing
  19.  
  20. '******************************************************************
  21.  
  22. '3 Methods to create a Sequential Text File
  23. Set ts = fso.CreateTextFile("C:\Test\TextFile1.txt", True)
  24.  
  25. Set ts = fso.OpenTextFile("C:\Test\Test.txt", ForWriting)
  26.  
  27. fso.CreateTextFile ("C:\Test\TestFile3.txt")
  28. Set fil = fso.GetFile("C:\Test\TestFile3.txt")
  29. Set ts = fil.OpenAsTextStream(ForWriting)
  30.  
  31. '******************************************************************
  32.  
  33. 'How to Open a File, use the 3 Write Methods to add data to
  34. 'the File, then Close the File
  35.  
  36. Set txtFile = fso.CreateTextFile("C:\Test\TextFile.txt", True)
  37. txtFile.Write ("This is just a test. ")     'Write a Line
  38.  
  39. 'Write a Line with a NewLine character
  40. txtFile.WriteLine ("Testing 1, 2, 3.")
  41. txtFile.Write "Test for the NewLine character!"
  42.  
  43. 'Write 3 NewLine characters to the File
  44. txtFile.WriteBlankLines (3)
  45. txtFile.Write "Just to see if we have 3 blank lines!"
  46. txtFile.Close
  47.  
  48. Set txtFile = fso.CreateTextFile("C:\Test\TextFile.txt", True)
  49. txtFile.Close       'Critical
  50.  
  51. 'Write a Line
  52. Set fil = fso.GetFile("C:\Test\Textfile.txt")
  53. Set ts = fil.OpenAsTextStream(ForWriting)   'ForAppending, ForReading
  54. ts.Write "Hello World"
  55. ts.Close
  56.  
  57. '******************************************************************
  58.  
  59. 'Opening a File for Reading
  60. Set fil = fso.GetFile("C:\Test\Textfile.txt")
  61. Set ts = fil.OpenAsTextStream(ForReading)
  62.  
  63. 'Read the contents of the File
  64. S = ts.ReadLine     'Reads 1st line into S
  65. S = ts.Read(25)     'Reads 25 characters into S
  66. S = ts.ReadAll      'Read entire file into S
  67.   MsgBox S
  68.   ts.Close
  69.  
Feb 23 '08 #1
0 4471

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4133
by: MatthewRoberts | last post by:
Howdy All, I am having difficulty with two-way communication across AppDomains in an attempt to dynamically script applications. Everything works as expected, except when using ByRef parameters. The below explanation is lengthy, but well worth the read. If you can help me, I'd gladly share this code which has greatly helped my development of extensible applications.
0
1579
by: fiona | last post by:
Catalyst Releases Scripting Editions of SocketTools Client and server-side development for Active Server Pages and PHP. Yucca Valley, CA, May 25, 2005 - Catalyst Development Corp (www.catalyst.com) - with over 10 years experience developing Internet components announced that Scripting and Secure Scripting Editions have been added to the SocketTools family of products. The Scripting Editions were designed specifically for client and...
17
4219
by: Karl Irvin | last post by:
To use the Textstream object, I had to set a Reference to the Microsoft Scripting Runtime. This works good with A2000 Is the Scripting Runtime included with A2002 and A2003 so the Reference won't be broken when my app is opened with those versions. Also is the Scripting Runtime included as part of the A2000 Runtime Engine which some of my customers use.
1
2346
by: Karl Irvin | last post by:
To use the Textstream object, I had to set a Reference to the Microsoft Scripting Runtime. This works good with A2000 Is the Scripting Runtime included with A2002 and A2003 so the Reference won't be broken when my app is opened with those versions. Also is the Scripting Runtime included as part of the A2000 Runtime Engine which some of my customers use.
14
2334
by: frostalicious | last post by:
Used VB.NET (on my client PC) to convert VB6 executable to .NET executable. Placed the .exe file on a network drive on my server. From client, ran .NET Wizards "Trust an Assembly" to make the .exe (on the network drive, on the server) "Full Trust." From the client, double-click on the ..exe (on the network drive, on the server) and it runs fine. So far, so good, but... On the server, where I've installed not VB.NET but .NET
1
1013
by: PaulieS | last post by:
Hi all. Am migrating a customer from IIS5 on W2K server to IIS6 on W2K3. Zipped all the websites and unzipped them to the identical locations on new server. Used IISMT to migrate metabase. Got this error when I tried to run a very basic page... code: <%@ LANGUAGE=vbscript %> <%
7
8871
ADezii
by: ADezii | last post by:
The next series of Tips will involve the Microsoft Scripting Runtime Library (Scrrun.dll). This Library is, in my humble opinion, one of the most useful and practical Libraries ever created. With the Scripting Runtime Library, you can retrieve information related to Drives, Folders, Files, and Text Files and set/retrieve certain Attributes of Drives, Folders, Files, and Text Files. Through Methods exposed by this Library, you can also manipulate...
0
5163
ADezii
by: ADezii | last post by:
This is the 2nd Tip in a series of Tips on the Microsoft Scripting Runtime Library. The 1st Tip related to Drives, while this Tip will concentrate on the Folders (Directories) in your PC and various methods to retrieve information from and how to manipulate them. The code should be self explanatory and basically documents itself. Should there be any questions related to this Topic, please feel free to ask. 'Accessing Folder Properties Dim...
0
5426
ADezii
by: ADezii | last post by:
This is the 3rd in a series of Tips dealing specifically with the Microsoft Scripting Runtime Library. In this Tip, we'll show you how to return specific Properties relating to Files, as well as various Methods to Copy, Move, and Delete them. The code is self explanatory, so I'll jump right in: Dim fso As FileSystemObject, fil As File 'Set fso = New Scripting.FileSystemObject OR Set fso = CreateObject("Scripting.FileSystemObject") ...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4635
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.