Copying Folders by Using the Shell Folder Object

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

WMI has several limitations when copying folders. For one, WMI provides no built-in method for tracking script progress. Although many system administration scripts have no user interface and are designed to run silently, there might be times when you would like to follow the progress of a script. For example, you might want to visually track a script that copies a large number of files and folders from one computer to another. That way, you can verify that the script is proceeding as expected.

A second limitation of the WMI Copy method is that a folder-copying script fails if the destination folder already exists. For example, you might want to periodically copy a folder named DiskPerformance to an archive folder named Monitoring. The first time you run the script, the copy operation will succeed. The next time you run the script, however, the folder Monitoring\DiskPerformance already exists. As a result, the copy operation fails unless you include extra code within the script to test for the existence of the DiskPerformance folder and then rename either that folder or the new folder being copied.

In addition, the WMI Copy method is an all-or-nothing method: It works only if the destination folder does not exist, and only if you copy over the entire source folder. WMI does not allow you to merge folders; that is, you cannot copy over new items without also deleting all the existing items in the destination folder.

The Shell Folder object includes a CopyHere method that allows you to overcome these limitations. First, the CopyHere method enables you to optionally display the Copying dialog box as the script runs. This dialog box is shown in Figure 11.5.

Figure 11.5   Copying Dialog Box

Copying Dialog Box

Another optional parameter allows you to copy a folder without overwriting the contents of the target folder. For example, suppose you have a target folder named E:\Scripts. You can copy a second folder named Scripts to that same location without losing the data in the original target folder. Instead, the new folder is automatically named Copy (1) of Scripts. If you copy another folder, it is named Copy (2) of Scripts. This allows you to copy folders from multiple computers without having to worry about providing those folders with unique names.

Finally, the CopyHere method supports merging folders. For example, suppose you have target and source folders with the file sets shown in Table 11.7.

Table 11.7   Sample File Sets Before Copying

Source FolderTarget Folder
SourceFile1

SourceFile2

SourceFile2

TargetFile1

TargetFile2

When you copy the source folder to the target folder, the file sets are merged, and the new folder contents look like those shown in Table 11.8.

Table 11.8   Sample File Sets After Copying

Source FolderTarget Folder
SourceFile1

SourceFile2

SourceFile2

TargetFile1

TargetFile2

SourceFile1

SourceFile2

SourceFile2

Note

  • If the target folder already has files named SourceFile1 and SourceFile2, those files are replaced by the versions being copied from the source folder.

To copy a folder using the CopyHere method, you need to create an instance of the Shell object, and then use the Namespace method to bind to the target folder (the location where the folder will be copied). You then call the CopyHere method, passing the path of the source folder (the folder being copied) as a required parameter. In addition, you can pass one or more of the optional constants shown in Table 11.9.

Table 11.9   Shell Folder CopyHere Constants

ConstantDescription
&H0&Displays a progress dialog box that shows the name of each file being copied.
&H4&Copies files without displaying a dialog box.
&H8&Automatically creates a new folder name if a folder with that same name already exists.
&H10&Automatically responds "Yes to All" to any dialog box that appears. For example, if you attempt to copy over existing files, a dialog box appears, asking whether you are sure you want to copy over each file. Selecting this option is identical to clicking Yes to All within that dialog box.
&H40&Preserves undo information. After the script has completed, you can open Windows Explorer and select Undo from the Edit menu to undo the copy procedure.
&H100&Displays a simple progress dialog box that does not show the name of each file being copied. Instead, it merely indicates that the copying procedure is in progress.

For example, this line of code copies the folder C:\Scripts while displaying a simple progress dialog box:

objFolder.CopyHere "C:\Scripts", &H100& 

Scripting Steps

Listing 11.12 contains a script that copies a folder to a new location, displaying the Copying dialog box as it carries out the procedure. To carry out this task, the script must perform the following steps:

  1. Create a constant named FOF_CREATEPROGRESSDLG and set the value to &H0&.
  2. Create a variable named ParentFolder and set the value to D:\Archive. This variable is used to indicate where the folder should be copied to.
  3. Create an instance of the Shell object.
  4. Use the Namespace method to create a Folder object representing D:\Archive. This is done by passing the variable ParentFolder to the Namespace method. If the folder D:\Archive does not exist, the procedure fails.
  5. Use the CopyHere method to copy C:\Scripts to D:\Archive. This creates a new folder, D:\Archive\Scripts, that contains the same files and subfolders as C:\Scripts. The CopyHere method uses two parameters:
    • The name of the folder being copied (C:\Scripts).
    • The optional constant FOF_CREATEPROGRESSDLG. This causes the Copying dialog box to be displayed as the contents of C:\Scripts are being copied.

Listing 11.12   Copying Folders Using the Shell Folder Object

1 2 3 4 5 
Const FOF_CREATEPROGRESSDLG = &H0& ParentFolder = "D:\Archive"  Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.NameSpace(ParentFolder)  objFolder.CopyHere "C:\Scripts", FOF_CREATEPROGRESSDLG

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net