In this section, you are going to check for the existence of the log file by modifying the script created in the "Creating Files" step-by-step exercise. If the file exists, you will overwrite it. If it does not exist, you will create it.
1. | Open Notepad or the script editor of your choice.
|
2. | Use Option Explicit and declare the following variables: LogFile, objFSO, and objFile.
|
3. | Create an assignment for the variable LogFile that will hold the name and path of your log file. The code will look like the following:
LogFile = "C:\FSO\fso.txt"
|
4. | Open Windows Explorer and ensure a folder called FSO and a text file called Fso.txt exist on your C drive. (Skip this step if you did this in the step-by-step exercise earlier.)
|
5. | Create a constant called ForWriting and set it equal to 2.
|
6. | Create a constant called ForAppending and set it equal to 8.
|
7. | Use CreateObject to create an instance of FileSystemObject. Set it equal to a variable called objFSO. Your code will look the following:
Set objFSO = CreateObject("Scripting.FileSystemObject")
|
8. | Use an If...Then...Else loop to implement the FileExists method of FileSystemObject. In this loop, test for the existence of LogFile. If the log file exists, append to it a line of text that indicates you appended to it and use the Now function so that you know when it ran. Your code will look like the following:
If objFSO.FileExists(LogFile) Then
Set objFile = objFSO.OpenTextFile(LogFile, ForAppending)
objFile.Write "appending " & Now
Else
|
9. | If the file does not exist, use the CreateTextFile command to create the log file. Assign the new file to the variable objFile. Your code will look like the following:
Set objFile = objFSO.CreateTextFile(LogFile)
|
10. | Use the Close method to close the file you just created. The code will look like the following:
|
11. | Use the OpenTextFile method to open the LogFile variable for writing. Set this equal to objFile. The following code illustrates this:
Set objFile = objFSO.OpenTextFile(LogFile, ForWriting)
|
12. | Use the Write method of objFile to write to the LogFile variable. Use the Now function to write the date and time this occurred. Use the following code as an example:
objFile.write "writing to new file " & Now
|
13. | End the If statement. Use End If to do this.
|
14. | Close the log file. Use objFile.Close for this purpose. Run your script. If you have problems, compare it with \My Documents\Microsoft Press\VBScriptSBS\ch06\OneStepFurther\osfLogIfExistA.vbs in the OneStepFurther folder.
|
15. | Delete lines created in steps 10 and 11.
|
16. | Save and run the script. Notice you do not need to close the file before writingVBScript knows if you created a file to which you want to write. If you have problems when you run this script, then compare it with \My Documents\Microsoft Press\VBScriptSBS\ch06\OneStepFurther\osfLogIfExistB.vbs.
|
To | Do This |
---|
Write to a file | Choose either the Write, WriteLine, or WriteBlankLines methods |
Include a carriage return and a line feed when you write to a line | Use the WriteLine method |
Append to a line when you write to it | Use the Write method |
Verify the existence of a file prior to writing to it | Use the FileExists method |
Read file attributes | Use the Attribute property of a File object |
Obtain a list of all files in a folder | Use the Files method once you have connected to a folder |
Connect to a folder | Use the GetFolder method |
Work with a single file from a collection of files | Iterate through the collection of files by using a For Each...Next loop |