Writing Text Files in WSH
(Page 1 of 4 )
Being able to write to text files can give your script a very powerful feature set. Among other things this will enable you to edit many program configurations and system settings, log a script’s output, or create other customized scripts on the fly. Please take the time to read the first article in this series in which I covered how to read text files in WSH.
By far the most common use of writing text files is for logging purposes. We will focus mostly on this application; however, you can easily use the methods provided here for any other purpose that involves writing to a plain text file.
Any time you want to manipulate files in WSH you should automatically think of the FileSystemObject. We’ll begin our script by making a call to its namespace. Familiarize yourself with the FileSystemObject because it is a commonplace in many scripts.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\ScriptLog.txt")
The next step is to create a text file. This is done with the FileSystemObject’s CreateTextFile method. The CreateTextFile method returns an object so we’ll need to use the Set command. To use the CreateTextFile method we must supply it with a file path and name.
That’s the simplest way, but suppose we were doing some more intricate coding. Maybe we’re using dynamic file paths. The following alternative uses a few more methods to create a text file that allows for a little more flexibility and control.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\path"
strFileName = objFSO.GetTempName
strFile = objFSO.BuildPath(strPath, strFileName)
Set objFile = objFSO.CreateTextFile(strFile)
objFile.Close
objFSO.DeleteFile(strFile)
This code snippet introduces a lot of methods. Let’s take a look at their syntax and then we’ll discuss what the code does.
object.CreateTextFile Name[, Overwrite[, Unicode]]
object.BuildPath Path, Filename
object.DeleteFile Name[, Force]
Now, let’s go back to our code snippet. We begin by connecting to the FileSystemObject. Next, we assign a path to the strPath string variable. We use the GetTempName method to assign a random name to the strFileName string variable. Now that we have a path and a file name, we use the BuildPath method to combine the two into a full path name which we assign to the strFile string variable. Still with me?
Now we can create the file. We use the CreateTextFile method like we did in our first example. Let’s take a second and talk about its syntax. The only required attribute is a full path to the file that we want created. However, there are a few optional attributes. Overwrite and Unicode are both Boolean attributes that determine whether we should overwrite any existing file (default is True) and whether or not to use Unicode (default is False, meaning to use ASCII) character set, respectively.
Using the Close method to disconnect from a file object will automatically save or discard any changes based on the Iomode used to open it.
After creating the text file, we have to use the Close method to release it before we can delete it with the DeleteFile method. Why close it? The file object is automatically opened for editing when it is created. If we don’t close it first we’re likely to run into some errors when we try to delete it while it’s in use. It’s also good to note that the DeleteFile method has an additional Force attribute that is a Boolean value. The Force attribute allows us to force deletion of Read-only files.
Next: Opening an existing text file >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer