Guy’s Scripting Ezine 79 – FSO Part 2 Append Text

Contents for Ezine 79 – FSO Append Text

 ♣

This Week’s Secret

This week I am taking my own advice and building the script gradually.  Last week we built stage 1, creating a folder and we also completed stage 2, creating a file.  So this week it’s the coup de grace, stage 3 appending text to the file we created earlier.

Scenario: We want to append text to file.

Let’s assume that we wish to write WMI information to a file.  Alternatively, we may wish to output Active Directory data into a text file.  In both cases we need to employ an FSO method called OpenTextFile.  (Not CreateTextFile)

Prerequisites

This is a script that will execute equally well on a Windows server or an XP machine.  To ensure that there are no permissions errors, I recommend that you logon as administrator.

Instructions for Creating a Folder

  1. Copy and paste the example script below into notepad
  2. Decide whether to change the value for strDirectory or strText.
  3. Save the file with a .vbs extension, for example: EditPreferences.vbs 
  4. Double click EditPreferences.vbs and check your new folder.

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

Here is the finished script, the fruits of two week’s labor.  It tests for the folder referenced by strDirectory, and then tests for the file name referenced by strFile.  If necessary, the script will even create the folder or the file.  Then we reach the key part, VBScript appends what ever value we set in strText to the end of the file.  Note it appends to the file and does not over-write existing lines.

‘ 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

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

Learning Points

Note 1: This is a complex example, which builds on last weeks scripts.  See here for a refresher on creating a folder and file.

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

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) strText.  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’s Challenge – Change 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 the file?  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 here

Summary for FSO – Append Text

In this example VBScript employs FileSystemObject to create an object called objFSO.  Once you have this FSO object then you can manipulate it with methods such as OpenTextFile.  One more factor, choose the correct CONST statement.  For Appending = 8, or for over-writing,  ForWriting =2.

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