PowerShell Ezine, Logon Scripts

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)


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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 http://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

Learning Points


For a top Script Editor try a free download at OnScript


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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

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.


 *


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.