Andrew Husking [ IT User ]  Joined: Fri Apr 13 2007, 06:36PM Posts: 23 | Time For the next installment of the VBScript intro course, i have been really busy\lazy lately, and i haven't put the next installment up.
So, This time we will look at some really useful things text files, and how to read\write and append to them.....
So lets start with reading a text file
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile ("c:\Test.txt", ForReading)
Do Until objTextFile.AtEndOfStream strText = StrText & objTextFile.Readline & vbCrlf
Loop
Msgbox StrText
ObjTextFile.Close The First line, Const ForReading = 1 allows us to tell VBscript what we want to do with the file, we understand it as ForReading, but to it, it knows reading as the number 1.
Set objFSO = CreateObject("Scripting.FileSystemObject") Creates an object that we can use to interact with the file system, this allows us to create, delete and do a few other things with files, but its going to allow us to open the text file we want so we can read it
Set objTextFile = objFSO.OpenTextFile ("c:\Test.txt", ForReading) This line opens the text file we want for reading and stores it in an object we can use later
Now, the reading of the text file
Do Until objTextFile.AtEndOfStream strText = StrText & objTextFile.Readline & vbCrlf Next Loop
The Do Until ObjTextFile.AtEndOfStream means that it will keep reading until it reaches the end of the text file (the loop command goes with this)
strText = StrText & objTextFile.Readline & vbCrlf Means that we are taking the previous lines read from the text file (StrText) adding the new line (ObjTextFile.Readline) and going to a new line (vbcrlf)
msgbox strText just displays what we just read from the text file in a msgbox
Then Lastly we close the text file
And thats how you read from a text file
Now to write to a new text file
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile("C:\Test2.txt") Set objTextFile = objFSO.OpenTextFile ("c:\test2.txt", ForAppending, True)
Name = inputbox ("What is your name?") Age = InputBox ("how old are you?") ObjtextFile.Writeline( name & " is " & age & " years old!" & vbcrlf) objTextFile.Close now for the break down...
Const ForAppending = 8, Basically the same as ForReading, we are assigning a String that we can understand to the number the computer understands
Again we create the FileSystem object using
Set objFSO = CreateObject("Scripting.FileSystemObject") Now, this is a unique way of doing it, but it still works nicely
Set objFile = objFSO.CreateTextFile("C:\Test2.txt") Set objTextFile = objFSO.OpenTextFile ("c:\test2.txt", ForAppending, True)
We are creating the new text file using the first line, then opening the text file for appending instead of writing, you can use code to just open it for writing but for this example i chose the creating then appending.
the second line just opens the text file for appending
Now for the last bit
Name = inputbox ("What is your name?") Age = InputBox ("how old are you?") ObjtextFile.Writeline( name & " is " & age & " years old!" & vbcrlf) objTextFile.Close
First we ask the user for some data to put into the text file (name and age), then we write that to a text file using ObjtextFile.Writeline( name & " is " & age & " years old!" & vbcrlf) Then Lastly we close the text file
And thats how you Write to a new text file, if you want to append, change the filename to a file that exists already and remove the line: "set objFile = objFSO.CreateTextFile("C:\Test2.txt") "
And thats about it this time, short and sweet i guess, post any questions comments you have...
Cheers Andrew
---------------------------------- Andrew Husking ERH Software Developer IT Admin, Hobby Programmer Microsoft Registered Partner
---------------------------------- A lack of planning on your part does not constitute an emergency on my part.
It's kind of fun to do the impossible.
The nice part about being a pessimist is that you are constantly being either proven right or pleasantly surprised.
Windows is a 32-bit extension to a 16-bit graphical shell for an 8-bit operating system originally coded for a 4-bit microprocessor by a 2-bit company that can't stand 1 bit of competition.
|