Guy’s Scripting Ezine 104 – Objects, Especially FSO

Guy’s Scripting Ezine 104 – Objects, Especially FSO

 ♣

This Week’s Secret

All modern scripting languages are object based, so in this respect, VBScript is not exceptional.  However, I am still in awe of the way thatVBScript 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:

  1. Active Directory, User, Group or Computer (LDAP//:domainame)
  2. File System Object (FSO) read or write to text files
  3. Network (As in network drive or printer)
  4. Shell (As in Windows Shell – Run a program such as Explorer)
  5. 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

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

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.

Recommended: Solarwinds’ Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Analyzer because it enables me to see WHO has permissions to do WHAT at a glance.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user’s access to a resource.  Give this permissions monitor a try – it’s free!

Download SolarWinds’ Free Permissions Analyser – Active Directory Tool

Example 1 Script: To Write to a Text File

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.

  1. Copy and paste the example script below into notepad or use a VBScript editor.

  2. One advantage of a good script editor such as OnScript is that you can see the color codes for the methods and values.

  3. Save the file with a .vbs extension, for example: FSO.vbs 

  4. 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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example 2 Write to a file.  Variation with Sub Routine.

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

  1. Experiment with other values for strPath. 
  2. 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)

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary – Objects

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.

See more about scripting files with VBScript

VBScripts  • Ezines  • WMI  • Create files  • File Open  • Ezine 12 FSO  • Permissions Analyzer

• Ezine 35 FSO  • Ezine 36 FSO II  • Ezine 43 File Create  • PowerShell Create Files

Ezine 78 Files  •Ezine 79 Files  • Ezine 104 FSO  •Ezine 139 Delete Temporary