Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Sometimes it is important simply to know whether a file exists. This might be done as part of a software inventory; for example, you might want to check all your mail servers and see whether a particular script file is present.
Knowing whether a file exists is also important when using scripts to carry out file system management tasks; as you might expect, attempting to copy, move, delete, or otherwise manipulate a file that does not exist will generate a run-time error. To avoid this kind of error, you can use the FileExists method to verify the existence of the file. The FileExists method requires a single parameter (the path to the file) and returns a Boolean value: True if the file exists; False if it does not.
The script in Listing 4.19 uses the FileExists method to verify the existence of the file C:\FSO\ScriptLog.txt. If the file exists, the script uses the GetFile method to bind to the file. If the file does not exist, the script echoes the message, "File does not exist."
Listing 4.19 Verifying the Existence of a File
|
|
You cannot use wildcard characters to verify whether a particular set of files (such as .txt files) exists in a folder, nor can you use wildcards to verify whether any files at all exist in a folder. For example, the following code sample does not result in an error but always returns the value False, regardless of how many files are in the folder:
WScript.Echo objFSO.FileExists(
"C:\FSO\*.*
")
If you need to verify the existence of a file based on some criteria other than the path, you have two options:
Send us your feedback | « Previous | Next » |