Previous Page
Next Page

One Step Further: Deleting Folders

In this section, you are going to delete the folders created in the previous step-by-step exercise.

1.
Open Notepad or your script editor of choice.

2.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch07\OneStepFurther\sbsCreateFolders.vbs script and save it as YourNamesbsDeleteFolders.vbs.

3.
In the Worker section of the script, inside the If objFSO.FolderExists statement, locate the line that says that the folder exists and is not to be created. Delete the portion that says "folder not created". The revised line looks like the following (make sure you remove the line continuation from the end of the revised line):

WScript.Echo(folderPath & folderPrefix & i & " exists.")

4.
Use the DeleteFolder method from the fileSystemObject to delete the folderPath & folderPrefix & i folder.

objFSO.DeleteFolder(folderPath & folderPrefix & i)

5.
Use WScript.Echo to print out that (folderPath & folderPrefix & i) was deleted. The code will look like the following:

WScript.Echo(folderPath & folderPrefix & i & " was deleted")

6.
In the Else portion of the If folder exists Then ... statement, delete the line that creates the folder. It will look like the following:

Set objFolder = objFSO.CreateFolder(folderPath & folderPreFix & i)

7.
Also in the Else portion of the If folder exists Then ... statement, change the line to read that the folder does not exist, rather than saying it was created. The revised line looks like the following:

WScript.Echo(folderPath & folderPrefix & i & " folder does not exist")

Chapter 7 Quick Reference

To

Do This

Prevent errors when creating or deleting folders

Use the folderExists method inside of an If...Then...Else construction

Bind to a folder

Use the GetFolder method

Provide access to the properties of a folder

Bind to the folder using GetFolder

Create a folder object

Use the CreateFolder method

Delete a folder

Use the DeleteFolder method

Obtain a collection of folders

Use the subFolders method

Copy a folder

Use the CopyFolder method

Move a folder

Use the MoveFolder method

Rename a folder

Move the folder to the same location while specifying a new name (use the MoveFolder method)



Previous Page
Next Page