Contents for Guy's Scripting Ezine 43 - File System Object
I love magicians. In particular I admire (from a distance) those
card-sharps that you see scurrying around at low grade race tracks.
One of the best tricks that I ever saw, was performed by a hustler who threw some sticks
and felt into the air; miraculously, they unfolded to form a card table.
Even more spectacularly, the hustler had dealt the first three cards before
his table hit the ground. What on earth has magic got to do with
VBScript? Well I love self contained scripts which magically unfurl to
conjure files out of thin air.
This week's script will produce a folder, fashion a file and then fill that file
with text. There is no need for you to tinker with variables to make this week's code work. However, if you wish, then you could uncomment one line and the folder will
disappear - magic!
This week's project
If you master the File Scripting Object, and it's not difficult, then you
can extend your VBScripts to include, reading .csv files and writing WMI output to .txt files. We will start by creating folders and adding files, then we are ready to write to those files.
CreateObject("Scripting.FileSystemObject") is the key command that
conjures up a file object. We can manipulate this FSO with VBScript
commands. What I want to do this week is
master the basic FSO methods, for example .CreateFolder .WriteLine.
Naturally, we start with building a folder to house our files. Once we
have created a file we can then start writing lines of text. More likely, we
need to store the output data, for example, when a WMI script interrogates
the operating system. In the case of import spreadsheets, it's the reverse. By that
I mean we want to read the cells into the script and then use that data to
modify user objects in Active Directory.
Scenario. You need a new folder with a new file. Perhaps this
is in preparation for writing WMI (Windows Management Information) data.
Instructions
- Copy and paste the script into notepad.
- Optionally, examine and amend the variables strFile and strDirectory
- Save the file with .vbs extension e.g. NewFolder.vbs
- Double click and observe the message boxes.
- Open Windows Explorer and check to see if your script created a new file.
' NewFolder.vbs
' VBScript to build a folder and create a file.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.6 - August 29th 2004
' ---------------------------------------------------------------'
Option Explicit
Dim objFSO, objFSOText, objFolder, objFile
Dim strDirectory, strFile
strDirectory = "c:\Guy"
strFile = "\Special.txt"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' The only point of this next line is for testing
objFSO.DeleteFolder(strDirectory)
' Create a Folder, using strDirectory on Line 10
Set objFolder = objFSO.CreateFolder(strDirectory)
Wscript.Echo "Just created " & strDirectory
' Create the File itself, using strFile on Line 11
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
Wscript.Quit
' End of example VBScript
Learning Points
Note 1: CreateObject("Scripting.FileSystemObject") is the command
that builds objFSO. The significance is that with objFSO we make the
File System Object, then we can use its magic to create a folder and finally, create a file.
Note 2: We can also delete a folder. The actual reason for
including .DeleteFolder in the test script is to prevent error 800A003A - File already
exists.
Scenario: Even I admit that FSO is rarely the sole purpose of
VBScript. However, my point is that when you master the .Methods and
objects which FSO offers, then your scripts benefit from the increased flow
of data. Write data output to text files, or open spreadsheets
and use the data to configure your Active Directory objects.
' WriteFile.vbs
' VBScript to build a folder, create a file and add text
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - August 29th 2004
' ---------------------------------------------------------------'
Option Explicit
Dim objFSO, objFSOText, objFolder, objFile, objTextFile
Dim strDirectory, strFile, strText
strDirectory = "c:\Guy"
strFile = "\Special.txt"
strText = "Seán is Magic"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Create a Folder, using strDirectory on Line 10
' objFSO.DeleteFolder(strDirectory)
' Note If..Exists. Then, Else ... End If construction
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
Wscript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Wscript.Echo strDirectory & strFile & " already exists"
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objFSOText = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSOText.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close
Wscript.Quit
' End of example VBScript
Learning Points
Note 1: This script uses the construction: If.. Exists,
Then, Else, ..End If. My reasoning for this construction is to cater for you running the
script several times without getting errors and without deleting the folder.
Tell the truth, I just love any excuse to experiment with the 'IF'
statement.
Note 2: The main point of this script is to open the file =
special.txt and then write in the strText. To achieve this goal we use
the .OpenTextFile method. This method relies on the statement: Const ForAppending
= 8.
Note 3: The action method is .WriteLine. In 'real' scripts
this would be repeated several times, for example .WriteLine for each value
the WMI extracts from the operating system. (Free
disk space, Operating System Version, or DHCP leases.
Note 4: Spot how the .Close method shuts the file gracefully.
FSO, (File System Object) has a surprisingly rich set of methods. I
use FSO when reading from spreadsheets and for writing WMI data to disk. It
is worth learning, not only how to create folders and files, but also how to
write data into that file.
See more on FSO - Whole Section Here
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|