Automatic CleanupOne nice way to use the script for creating folders is to reuse it and modify it to delete folders. The idea here is that when you use scripts to create folders and then use them to delete folders, you have basically enabled automatic cleanup after your operations are complete. Just the Steps
Deleting a FolderDeleting a folder requires a connection to FileSystemObject. Once the connection to FileSystemObject is established, you use the DeleteFolder method to delete the folder. This is illustrated in the following script, DeleteBasicFolder.vbs. Notice that the big difference between creating a folder and deleting a folder is that the line in which the folder is deleted does not begin with Set. Rather than use Set, you simply include objFSO with the DeleteFolder method and then the path to the folder you will delete. DeleteBasicFolder.vbs Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.DeleteFolder("c:\fso") Deleting Multiple FoldersIt is just as easy to delete multiple folders as a single folder because the syntax is the same: Make a connection to FileSystemObject and then call the DeleteFolder method. In the DeleteMultiFolders.vbs script that follows, to make the script clean up after itself, you have to make only three changes to CreateMultiFolders.vbs. Imagine how easy it would be to run CreateMultiFolders.vbs when your school year begins to create individualized student workspaceand then when the school year ends, run DeleteMultiFolders.vbs with three minor modifications to reclaim the storage space used by students during the school year. What are the modifications? There are no modifications in either the Header information or the Reference information section of the script. In the Worker information section of the script, you delete Set objFolder = and then change CreateFolder to DeleteFolder. In the Output information section of the script, you change folders created to read folders deleted. DeleteMultiFolders.vbs Option Explicit Dim numFolders Dim folderPath Dim folderPrefix Dim objFSO Dim objFolder Dim i numFolders = 10 folderPath = "C:\" folderPrefix = "TempUser" For i = 1 To numFolders Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.DeleteFolder(folderPath & folderPreFix & i) Next WScript.Echo(i - 1 & " folders deleted")
|