Lets try something a little more interesting. Files.
1. Make a vbs-file and add these lines
Option Explicit
' Declare the variables
Dim oFileSystemObject
Dim oTestFile
Set oFileSystemObject = CreateObjec("Scripting.FileSystemObject")
' Write to the file
Set oTestFile = oFileSystemObject.CreateTextFile("C:\HelloWorld.txt")
oTestFile.WriteLine "Hello World!"
oTestFile.Close
set oTestFile = nothing
' Read from the file
set oTestFile = oFileSystemObject.OpenTextFile("C:\HelloWorld.txt")
While not oTestFile.AtEndOfStream
wscript.echo oTestFile.Readline
wend
oTestFile.Close
set oTestFile = nothing
set oFileSystemObject = nothing
2. Save and run the file.
You should see a dialog with Hello World! Furthermore you should also could see a newly created textfile which contains Hello World!
Now, lets see how this works:
First we instantiate a Scripting.FileSystemObject, which is a standard ActiveX component. It can be used to a lot of the basic filehandling commands. Copying, moving, deleting, creation etc.
We then use its CreateTextFile method to instantiate a textstream object (oTestFile). That object can be used to write lines to the file.
Then we close the file so we can open it again and finally we get rid of the object from the memory by setting it to nothing.
To get som output, we use the oFileSystemObject to open the file again into the oTestFile textstream. We use the ReadLine method until the file is at its end to write all the lines in the file. And again close it and get rid of the objects.
A little tip you can try is to write more lines in the file and see that it reads them all.
Until next time.. Say hello to the world from me.
torsdag den 17. september 2009
Abonner på:
Kommentarer til indlægget (Atom)
Ingen kommentarer:
Send en kommentar