Previous Page
Next Page

Automatic Cleanup

One 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

To delete a folder

1.
Implement FileSystemObject by using CreateObject.

2.
Use the DeleteFolder command to delete the folder.


Deleting a Folder

Deleting 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 Folders

It 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")

Quick Check

Q.

To delete a folder, what two components are required?

A.

You need a connection to FileSystemObject, and you need to use the DeleteFolder method.

Q.

What is a positive aspect of deleting folders programmatically?

A.

A positive aspect of deleting folders programmatically is that you can do so by easily modifying the script used to create the folders.

Q.

What are two situations in which creating folders and deleting folders programmatically would be useful?

A.

Creating folders programmatically is useful for schools that need to create a lot of student home folders at the beginning of the school year and then delete them at the end of the year. The same technique is useful for companies when they bring in temporary workers.


Automating cleanup

1.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch07\EfficientFolderLogging.vbs script and save it as YourNameEfficientFolderLoggingDelete.vbs.

2.
Create a new subroutine under the subLogging subroutine. Call it subDelete. Make sure you close out the subroutine, as seen below:

Sub subDelete
End sub

3.
Copy everything from the DeleteMultiFolders.vbs script except the Option Explicit line. Paste it into the subDelete subroutine, as seen below:

Sub subDelete
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")
End sub

4.
Change the numFolders variable from 10 to 100.

numFolders = 100

5.
Call the subDelete subroutine from the last line of the subLogging subroutine.

6.
Save and run your script. If it does not work, then compare it to the EfficientFolderLoggingDelete.vbs script.


Previous Page
Next Page