Guy’s Scripting Ezine 79 – FSO Part 2

Contents for Ezine 79 – FSO Write text to File

Challenge – Change from Append to Over-write

I throw down the gauntlet, instead of appending data can you make the script delete the existing content and just write strText?  To help you get started, I changed
Const ForAppending = 8 to Const ForWriting = 2.  What else should I do?  For extra help See ‘Out Takes on the website.

Here is the original Script.

Example – Create the Folder, create the File and Write to that File

Here is the finished script.  The fruit of our labours.  It tests for the folder reference by strDirectory, tests for the file.  If necessary the script even creates the folder and file.  Then the key part, it appends what ever is in strText to the file.  Not it adds to the file and does not over-write.

 

 

‘ EditPreferences.vbs
‘ Sample VBScript to write to a file.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Ezine 79 Version 2.4 – July 2005
‘ ————————————————————-‘

Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\Ezine"
strFile = "\GuySays.txt"
strText = "Edit your Ezine Preferences"

‘ Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

‘ Check that the strDirectory folder exists
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)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = Nothing

‘ OpenTextFile Method needs a Const value
‘ ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)

‘ Key Section to write the strText to the file.
‘ Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close

‘ Guy’s idea for testing your script.
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit

‘ End of VBScript to write to a file

 

The answer to the challenge to over-write text was to add:

Not only: Const ForWriting = 2

But Also: Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForWriting, True)

Example – Create the Folder, create the File and Write to that File

Here is the finished script.  The fruit of our labours.  It tests for the folder reference by strDirectory, tests for the file.  If necessary the script even creates the folder and file.  Then the key part, it appends what ever is in strText to the file.  Note this script over-writes (and not appends).

 

 

 

‘ EditPreferences.vbs
‘ Sample VBScript to over write text to a file.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Ezine 79 Version 3.1 – July 2005
‘ ————————————————————-‘

Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\Ezine"
strFile = "\GuySays.txt"
strText = "Edit your Ezine Preferences"

‘ Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

‘ Check that the strDirectory folder exists
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)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = Nothing

‘ OpenTextFile Method needs a Const value
‘ ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForWriting = 2

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForWriting, True)

‘ Key Section to write the strText to the file.
‘ Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close

‘ Guy’s idea for testing your script.
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit

‘ End of VBScript to write to a file

 

Learning Points

Note 1: This is a complex script built on last weeks scripts.  See for a refresher on creating a folder and file.

Note 2: As with many file scripts, the first task is to create an object that we can then use to manipulate the text, here is the classic FSO command:
Set objFSO = CreateObject("Scripting.FileSystemObject"). Once we create objFSO we can GetFolder and CreateTextFile.

Note 3: The central point of this script is the OpenTextFile method. What we must also do is control whether to read, write or append (as in this case). Examine carefully the CONST statement. This example uses
Const ForAppending = 8.

Note 4: Without adding these two lines the VBScript fails with a permissions error:
set objFile = nothing
set objFolder = nothing

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

Challenge – Change from Append to Over-write

I throw down the gauntlet, instead of appending data can you make the script delete the existing content and just write strText?  To help you get started, I changed
Const ForAppending = 8 to Const ForWriting = 2.  What else should I do?  For extra help See ‘Out Takes on the website.

Summary for FSO

With VBScript you can create a FileSystemObject.  Once you have the FSO object then you can manipulate it with methods such as .CreateFolder and .CreateTextFile.  Incidentally these examples are a change from the usual GetObject or create network objects.