Previous Page
Next Page

Moving Folders

Copying folders is a very safe operation because nothing happens to the original data. Copy operations are often used to present a consolidated view of data (such as copying log files) or to create redundant data for backup purposes (as in the case of VBScript book manuscripts). Moving folders, on the other hand, can be done to free up disk space, or can be done simply because two copies of the data are neither required nor desired. If a copy operation fails halfway through, you simply end up with an extra copy of half your data. If, on the other hand, a move operation fails halfway through, to have even one complete set of information, you have to go to the destination machine and move your data back. Because of this, with important data, I always copy, verify, and then delete. For files I am not concerned about, I perform a move.

To perform a move operation, use the MoveFolder method of FileSystemObject. The next script you look at, MoveFolder.vbs, illustrates the MoveFolder method. Unlike the CopyFolder method, MoveFolder has only two parameters: the source and the destination. The overwrite parameter, which enables overwriting an existing folder during a move operation, is not implemented. It's common to move folders between drives, but you can also use the MoveFolder method to move folders on the same drive, and in effect, you get the ability to rename a folder. This is required, as there is no rename folder method in the FileSystemObject. In MoveFolder.vbs, you do exactly that. You begin with a source folder called c:\fso, and the destination folder is c:\fso2.

MoveFolder.vbs

Set objFSO = CreateObject ("scripting.fileSystemObject")
objFSO.MoveFolder "c:\fso","c:\fso2"

Important

If you run the MoveFolder.vbs script, your c:\fso folder becomes c:\fso2. It is important to rename the c:\fso2 folder back to C:\fso because the following procedure relies upon it being set to c:\fso.


Walking through the directory

1.
Open \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs. Save your script as YourNameRecursiveListOfFolders.vbs.

2.
On the first line of your script that is not commented, add the words Option Explicit.

3.
Declare two variables: strTarget and objFSO. The script to this point should look like the following:

Option Explicit
'On Error Resume Next
Dim strTarget  'the place to begin recursive folder listing
Dim objFSO           'the file system object

4.
In the Reference section of the script, use strTarget to point to a subfolder that does not exist. I used the following folder:

strTarget = "c:\fso\mred"

5.
On the next line, create an instance of the FileSystemObject and assign it to the objFSO variable. It will look like the following:

Set objFSO = CreateObject("Scripting.FileSystemObject")

6.
Go to the bottom of your script and create a subroutine called subCheck. Make sure you go ahead and close it out as well by using End sub.

Sub subCheck
End sub

7.
Inside the subroutine, declare some variables that will be used for a msgBox function. I used strTitle, strPrompt, and errRTN. These properties are illustrated in Figure 7-1.

Dim strPrompt  'msgBox prompt
Dim strTitle   'title of msgBox
Dim errRTN     'return code from the msgBox function

Figure 7-1. Message box title, prompt, and button


8.
Build up a string to use for the prompt portion of the msgBox function. Include the name of the directory and the fact it does not exist, and ask if the user wants to create the folder. Capture the return from the function in the errRTN variable. My code to do this looks like the following:

strPrompt = strTarget & " Does not exist." &_
         vbNewLine & "Would you like to Create it?"

9.
Build up a string to use for the title of the message box. Include the name of the folder and identify that it does not exist.

strTitle = strTarget & " not found!"

10.
If the folder exists, then we want to call the subRecursiveFolders subroutine. When we do this, we want to pass the Folder object from GetFolder. The code to do this is seen below:

If objFSO.FolderExists(strTarget) Then
         subRecursiveFolders objFSO.GetFolder(strTarget)

11.
If the folder does not exist, then we want to display a message box and ask if the user wants to create the missing folder. Use the strPrompt variable and the strTitle variable for the prompt and the title of the message box. Use the intrinsic button constant vbYesNo to display yes and no buttons. Add to this constant the vbQuestion constant to cause the message box to display a question mark. Capture the return code in the errRTN variable. The code to do this is seen below:

errRTN = msgBox(strPrompt,vbYesNo+vbQuestion,strTitle)

12.
If the errRTN code is equal to vbYes, then we will create the folder. If not, we do not do anything. This code is seen below:

If errRTN = vbYes Then
   objFSO.CreateFolder(strTarget)
End If

13.
Close out the folder exists If...Then loop by using End If.

Warning

The coding will actually look a little strange at the end of the subCheck subroutine. You will have End If, a new line, End If, a new line, and End sub. Tab your code over to make it easy to read. Leaving out any of these statements will result in errors.

14.
At the bottom of your script, begin a new subroutine called subRecursiveFolders. You will pass a value to this subroutine called folder. Inside the subroutine, anything that is passed as a parameter to the subroutine will be known as a folder inside the subroutine. Make sure you close out the subroutine. The code to do this looks like the following:

Sub subRecursiveFolders(Folder)
End sub

15.
Dim a variable inside the subroutine called objFolder. This variable will be used to hold an individual folder object when iterating through the collection of subfolders. The code to do this looks like the following:

Dim objFolder

16.
Use the subFolders method to obtain a collection of subfolders. Use For...Each...Next to walk through this collection. Call each individual folder in the collection objFolder, as seen below:

For Each objFolder In Folder.subFolders

17.
Print out the path to each folder in the collection and then call the subRecursiveFolders subroutine while passing objFolder as a parameter. The code to do this looks like the following:

 WScript.Echo objFolder.Path
  subRecursiveFolders objFolder
Next

18.
Call the subCheck subroutine by placing the name of the subroutine on the line after the FileSystemObject is created. This is seen below:

subCheck

19.
Save and run the script. It should either produce a listing of folders or offer to create a nonexistent folder. If it does not, then compare your script with the \My Documents\Microsoft Press\VBScriptSBS\ch07\RecursiveListOfFolders.vbs script.


Previous Page
Next Page