Guy's Scripting Ezine 104 - Objects, Especially FSO
All modern scripting languages are object based, so in this respect, VBScript is not exceptional. However, I am still in awe of the way that
VBScript can call upon Windows Script Host for an endless supply of objects. To me, each type of object has its own personality. This is because each object has different properties and is manipulated by
different methods. Here are the 5 main types of VBScript object:
- Active Directory, User, Group or Computer (LDAP//:domainame)
- File System Object (FSO) read or write to text files
- Network (As in network drive or printer)
- Shell (As in Windows Shell - Run a program such as Explorer)
- WMI (Disk, memory, event viewer)
My secret is that I do not study these objects types out of a desire for theoretical knowledge, but because I forget which object has which capabilities. For example, I was trying to be flashy
and launch the Windows Explorer; I tried: set objExp = CreateObject("WScript.Network") objExp.Run ("Explorer" & " " & strPath ) Can you see what's wrong? CreateObject("WScript.Network"),
should be, CreateObject("WScript.SHELL"). I foolishly choose an object with the wrong properties. The truth is I solved the problem by doing what you would have done, I searched for an old
script where I knew the .Run method had worked. Once I saw that the object was .Shell it was easy to correct my mistake.
This week's mission is to examine the variety of VBScript object types, and then hone in on one type: File System Object. To recap, objects have properties and we control what the object does with methods.
To take an example, from this week's script, we can append data to a FSO (File System Object) with the .writeline method.
What I want to do is get you started with the simplest FSO script, namely just to write data into a file. This technique is particularly handy when you need to record the results in a permanent file.
One case in point is a WMI script, where you do not want to echo the data to the screen, instead you want to store the computer data on disk.
Other jobs for FSO include reading from spreadsheets, for example, if you are building zillions of users from data held in Excel.
This week's mission is to examine the variety of VBScript object types, and then hone in on one type: File System Object. To recap, objects have properties and we control what the object does with
methods. To take an example, from this week's script, we can append data to a FSO (File System Object) with the .writeline method.
What I want to do is get you started with the simplest FSO script, namely just to write data into a file. This technique is particularly handy when you need to record the results in a permanent file.
One case in point is a WMI script, where you do not want to echo the data to the screen, instead you want to store the computer data on disk.
Other jobs for FSO include reading from spreadsheets, for example, if you are building zillions of users from data held in Excel.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
Pre-requisites You need a folder to hold the file. I choose E:\ Files. Therefore, to make sure your script works, either create a folder called 'Files' on your 'e: drive', or better
still, change the value of strPath to reflect your preferred folder. This script should work on any Windows, machine, this week you do not need Active Directory.
Instructions for modifying the properties of computer objects. Copy and paste the example script below
into notepad or use a VBScript editor.
One advantage of a good script editor such as OnScript is that you can see the color codes for the methods and values.
Save the file with a .vbs extension, for example: FSO.vbs
Double click FSO.vbs, the check the contents of the file specified by strFile.
' FSO.vbs ' VBScript to write to a file ' Author Guy Thomas http://Computerperformance.co.uk/ ' Version 1.2 - February 2006 '
------------------------------------------------------' Option Explicit Dim objFile, objExp Dim strPath, strFile, strFilePath, strGuyFile
strPath = "e:\files\" strFile = "Guy.txt"
strFilePath = strPath & strFile
' FSO creates the object called - objFile Set objFile = CreateObject("Scripting.FileSystemObject") Set strGuyFile = objFile.CreateTextFile(strFilePath, True)
' Method called .WriteLine to add data. strGuyFile.WriteLine("This was made using an FSO Object.") strGuyFile.Close
' Bonus section to actually open the file set objExp =
CreateObject("WScript.Shell") objExp.Run (strFilePath )
WScript.Quit
Learning Points
Note 1: This script creates a File System Object and not a network or Active Directory object. The script then puts objFile to work and as a result, a text file is born. We control the
name of this file with strGuyFile.
Note 2: The situation is this, we have created an empty file. Now we select the .WriteLine method to enter the data into the file, just as though we had typed the letters on the keyboard.
Note 3: It is interesting that in order to open the Windows Explorer, we call for a second type of object; we select a Shell objects because it has the .Run method. Neither FileSystemObjects
nor network objects support the .Run method.
Just a
small variation on the Write to File method; this example employs a sub routine to launch the Windows Explorer.
' FSO_Wex.vbs ' VBScript to write to a file ' Author Guy Thomas http://Computerperformance.co.uk/ ' Version 1.3 - February 2006 '
------------------------------------------------------' Option Explicit Dim objFile, objExp Dim strPath, strFile, strFilePath, strGuyFile
strPath = "e:\files\" strFile = "GuySub.txt"
strFilePath = strPath & strFile Set objFile = CreateObject("Scripting.FileSystemObject") Set strGuyFile = objFile.CreateTextFile(strFilePath, True) strGuyFile.WriteLine("This used Sub Explorer().")
strGuyFile.Close
Call Explorer
WScript.Quit
' Sub routine to launch Windows Explorer sub Explorer() set objExp = CreateObject("WScript.Shell") objExp.Run ("Explorer" & " " &
strPath )
End Sub
Learning Points
Note 0: Invest in OnScript and see how color coding makes your scripts easier to follow and to troubleshoot.
Note 1: The only real point of the second script is to demonstrate my latest craze of using sub routines. In truth, sub routines are only beneficial if you reuse the code later in the script.
My personal indulgence is to use sub routines in constructing complex scripts from a series of modules or sub routines.
Challenges- Experiment with other values for strPath.
- How about adding a variable to control the text that you write in the file? For example, declare strText, then control its value with strText = "This was made using an FSO Object." Finally,
amend strGuyFile.WriteLine(strText)
VBScript can create an endless stream of objects. Each object has its own properties and thus is suitable for certain scripting tasks, but not others. File System Object is useful addition to script where you
need to store the output in a text file.
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.
|