Guy’s Scripting Ezine 43 – File System Object

Contents for Guy’s Scripting Ezine 43 – File System Object

 ♣

This Week’s Secret

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!

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

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.

Example 1 Build a folder and create a file

Scenario.  You need a new folder with a new file.  Perhaps this is in preparation for writing WMI (Windows Management Information) data.

Instructions

  1. Copy and paste the script into notepad.
  2. Optionally, examine and amend the variables strFile and strDirectory
  3. Save the file with .vbs extension e.g. NewFolder.vbs
  4. Double click and observe the message boxes.
  5. 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 https://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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2 Writing data into a file that you have created.

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 https://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 = "Seing 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.

Summary

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