|
A General-Purpose File-Deletion Script Continued from File Management Objects The idea is simple: Once you have a FileSystemObject, you can get at a specific folder by giving the folder specification to the GetFolder method. Once you have a folder object, you can get at all the files in the folder via its Files property. This returns what VBScript calls a collection. And once you have a collection, you can run through the items in it using a construct like this: For Each Item In TheCollection ' do what you want to the item Next For example, the following code would try to delete all files in a folder whose extensions are stored in a variable named TheExtension, by checking that the rightmost three characters match the extension: Set TheFiles = AFolder.Files For Each AFile In TheFiles If UCase(Right(AFile.Name, 3)) = TheExtension Then AFile.Delete End If Next Note that as written, there are two problems with this code that make it less than ideal. The first is that, for example, you can't delete a file that is in use. You therefore need to catch any errors and tell VBScript to disregard them. This is done with the On Error Resume Next statement. The next problem is that in Win32, file extensions are no longer restricted to three characters (you can use, for example, .jpeg, .html, and .java), so you'll probably want to allow for longer extensions. If you want to go beyond deleting TMP and BAK files, the easiest solution is to use the GetExtensionName method of the FileSystemObject. This takes the path of the file that you want to work with and returns the extension. Then, all you have to do is compare the value you obtain from this method call to the extension you want to delete, like so: Sub KillFilesWithExtensionIn (AFolder,TheExtension) Dim AFile, TheFiles On Error Resume Next Set TheFiles = AFolder.Files For Each AFile In TheFiles If UCase(FSO.GetExtensionName (AFile.Path)) = TheExtension Then AFile.Delete End If Next End Sub Now comes the tricky part: We want to be able to apply this routine to all the subfolders of a given directory and then continue the process on all subfolders of subfolders, and so on, until we have traversed the whole chain of sub-sub-sub-subfolders of a given folder. Such a routine is called a recursive routine. More precisely, a recursive routine is one that calls itself. Here's the basic procedure: We kill all of the appropriate files in the folder that is first passed to the routine. Next we find all the subfolders of a given folder using the SubFolders property of the Folder object. (This gives us a collection of Folder objects.) Then we kill all of the appropriate files in each subfolder, by calling the previous routine on each subfolder in the collection of Folders returned by the SubFolders property. Sub WorkWithSubFolders(ByVal AFolder, ByVal TheExtension) Dim MoreFolders, TempFolder KillFilesWithExtensionIn AFolder, TheExtension Set MoreFolders = AFolder.SubFolders For Each TempFolder In MoreFolders WorkWithSubFolders TempFolder, TheExtension Next End Sub Note the line inside the routine that calls the routine: WorkWithSubFolders TempFolder, TheExtension This is the line that makes the routine recursive. VBScript gives the routine the current-folder object TempFolder and starts the process again. It handles all the management that unwinds the chain of calls to the subfolders of subfolders of subfolders, without your needing to worry about the process. Next: A Few Details Published as Operating Systems in the 6/30/98 issue of PC Magazine. |
|
TOP |
Copyright (c) 1998 Ziff-Davis Inc. |