Deleting a File
Microsoft® Windows® 2000 Scripting Guide
The ability to delete files by using the FileSystemObject enables you to create scripts that can automatically perform tasks such as disk cleanup operations. For example, you might have a script that periodically searches for and deletes all temporary files (files with the .tmp file name extension). Alternatively, the script might delete files based on some other criteria, such as those that have not been accessed in the past six months, or those with a particular file name extension (such as .bmp or .mp3).
You can delete a file by creating an instance of the FileSystemObject and then calling the DeleteFile method, passing the path to the file as the parameter. For example, the script in Listing 4.20 deletes the file C:\FSO\ScriptLog.txt.
Listing 4.20 Deleting a File
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.DeleteFile("C:\FSO\ScriptLog.txt")
By default, the DeleteFile method will not delete a read-only file; if fact, a run-time error will occur if you attempt to delete such a file. To avoid errors, and to delete read-only files, add the optional Force parameter. When the Force parameter is set to True, the DeleteFile method can delete any file. For example, this line of code deletes the file ScriptLog.txt, even if that file is marked as read-only:
Deleting a Set of Files
There might be occasions when you require a script to delete a single, specified file. More likely, though, you will want to use scripts to delete multiple files. For example, at the end of the week, you might want to delete a set of log files that has been archived or delete all the temporary files that have been created but not removed.
Wildcard characters allow you to delete a set of files within a single folder. However, you cannot use the DeleteFile method to directly delete files from multiple folders. Instead, your script needs to iterate through the folders and use the DeleteFile method to individually delete the files in each folder. To delete files from multiple folders in a single operation (for example, to delete all the .TMP files stored anywhere on a computer), you should use WMI instead of the FileSystemObject.
To delete a set of files, call the DeleteFile method, supplying the path of the folder and the wildcard string required to delete files based on name or file name extension. For example, this line of code deletes all the .doc files in the C:\FSO folder:
This line of code deletes all the files with the letters log somewhere in the file name:
As noted previously, the DeleteFile method does not delete any documents marked as read-only. If a script attempts to delete a read-only document, a run-time error will occur, and the DeleteFile method will stop, even if the script uses the On Error Resume Next statement. For example, suppose you are trying to delete 1,000 .txt files, and one of those files is marked as read-only. As soon as the script attempts to delete that file, an error will occur, and the DeleteFile method will stop. The script will make no attempt to delete any other files, even though none of them are read-only.
Because of that, you can use an optional second parameter, Force, that can be set to True. When the Force parameter is set to True, the DeleteFile method can delete read-only documents. When the Force parameter is set to False (the default value), the DeleteFile method cannot delete read-only documents.
The script in Listing 4.21 deletes all the .txt files in the folder C:\FSO. To ensure that all files, including read-only files, are deleted, the Force parameter is set to True using the constant DeleteReadOnly.
Listing 4.21 Deleting a Set of Files
1 2 3
Const DeleteReadOnly = True Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.DeleteFile("C:\FSO\*.txt"), DeleteReadOnly
What if you want to delete all files except those marked as read-only? In that case, you can retrieve the complete set of files by using the Folder object Files property. You can then cycle through the collection, check to see whether each individual file is read-only, and, if it is not, delete the file.