Contents for Ezine 79 – FSO Write text to FileChallenge – Change from Append to Over-writeI 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 Here is the original Script. Example – Create the Folder, create the File and Write to that FileHere 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 Option Explicit ‘ Create the File System Object ‘ Check that the strDirectory folder exists If objFSO.FileExists(strDirectory & strFile) Then set objFile = nothing ‘ OpenTextFile Method needs a Const value Set objTextFile = objFSO.OpenTextFile _ ‘ Key Section to write the strText to the file. ‘ Guy’s idea for testing your script. 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 _ Example – Create the Folder, create the File and Write to that FileHere 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 Option Explicit ‘ Create the File System Object ‘ Check that the strDirectory folder exists If objFSO.FileExists(strDirectory & strFile) Then set objFile = nothing ‘ OpenTextFile Method needs a Const value Set objTextFile = objFSO.OpenTextFile _ ‘ Key Section to write the strText to the file. ‘ Guy’s idea for testing your script. WScript.Quit ‘ End of VBScript to write to a file
Learning PointsNote 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: 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 Note 4: Without adding these two lines the VBScript fails with a permissions error: Guy Recommends: The Free IP Address Tracker (IPAT)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-writeI 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 Summary for FSOWith 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. |