Thursday, July 10, 2014
How to Read the Contents of a Text File Using VBScript
1. Launch Notepad and create a text file. Type the following message into the text file:If you get this message,
you have successfully
completed this
programming task.
2. Save the file you just created to the “C:\Temp” folder on your hard drive and name it “TextToRead.txt”.
3. Create a second new text document in Notepad.
4. Copy and paste the following code into the file you created in the preceding step:Option ExplicitConst conForReading = 1'Declare variables
Dim objFSO, objReadFile, contents'Set Objects
Set objFSO = CreateObject('Scripting.FileSystemObject')
Set objReadFile = objFSO.OpenTextFile('C:\Temp\TextToRead.txt', 1, False)'Read file contents
contents = objReadFile.ReadAll'Close file
objReadFile.close'Display results
wscript.echo contents'Cleanup objects
Set objFSO = Nothing
Set objReadFile = Nothing'Quit script
WScript.Quit()
5. Click “File” on the Notepad toolbar and select “Save” from the context menu. Save the file with your VBScript code as “C:\Temp\ReadTextFile.vbs” and then exit Notepad.
6. Run your VBScript by browsing to the “C:\Temp” folder on your hard drive and double-clicking on the “ReadTextFile.vbs” file. The message that you entered in the text file in Step 1 will appear in a small pop-up window, confirming that your VBScript program has successfully opened and read the file. Click “OK.”
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment