Export (0) Print
Expand All

Copying a Folder and Its Contents

Microsoft® Windows® 2000 Scripting Guide

The ability to copy a folder, and every item contained within that folder, is important in system administration. Sometimes you need to copy folders in order to create backups; by having the same folder on Computer A that you have on Computer B, you are less likely to experience data loss should Computer B unexpectedly fail. At other times, you might want to deploy all the files contained in a particular folder to a large number of computers. Using a script to copy this folder to each computer is far more efficient than performing the task manually.

The CopyFolder method allows you to copy a folder and its contents to another location. When used without any wildcard characters, the CopyFolder method functions like the Xcopy /E command: It copies all the files and all the subfolders, including any empty subfolders. The CopyFolder method requires two parameters:

  • Source folder (the folder being copied). This folder can be specified either as a local path (C:\Scripts) or as a UNC path (\\helpdesk\scripts).

  • Destination folder (the folder that will hold the copied information). This folder can also be specified either as a local path or as a UNC path. If the destination folder does not exist, the script automatically creates the folder.

In addition, the CopyFolder method accepts an optional third parameter, Overwrite. When this parameter is set to True, the default setting, the script overwrites any existing folders in the destination folder. For example, if you are copying a folder named Scripts, and the destination already contains a folder by that name, the destination folder will be replaced by the newly copied information. By setting this parameter to False, the script will not overwrite existing information and instead generates a run-time error.

note Note

  • The CopyFolder method stops the moment it encounters an error, even if the script contains an On Error Resume Next statement. For example, suppose the script has 100 subfolders to copy, and CopyFolder successfully copies 3 of those subfolders before encountering an error. At that point, the CopyFolder method ends and the script fails; the script will not even attempt to copy the remaining 97 subfolders.

The script in Listing 4.9 uses the CopyFolder method to copy the contents of C:\Scripts to C:\FSO, overwriting any existing files in the destination folder. Note that this will not result in a folder named C:\FSO\Scripts; instead, the folder C:\FSO will simply contain the same files and folders as C:\Scripts. To create a folder named C:\FSO\Scripts, you would need to specify C:\FSO\Scripts as the destination folder.

Listing 4.9 Copying a Folder

  
1
2
3


Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Scripts" , "C:\FSO" , OverWriteFiles

note Note

  • Because CopyFolder is a single operation, there is no way to track its progress; you simply have to wait until the operation has finished. If you want to monitor the progress of the copy command, you should use the Shell Application object instead. This object is discussed in "Files and Folders" in this book.

Using Wildcards to Copy Folders

The CopyFolder command copies the files stored in a folder as well as the files stored in any subfolders of that folder. This can be a problem; after all, what if you want to copy only the files in C:\FSO and not all the files stored in C:\FSO\Subfolder1, C:\FSO\Subfolder2, and C:\FSO\Subfolder3?

Unfortunately, there is no straightforward method for copying the files in a parent folder without also copying the files stored in child folders. You can use wildcard characters to limit the set of subfolders that are copied; for example, the following command copies only those folders that start with the letters log. However, when you use wildcard characters, no files other than those in the specified folders will be copied, not even files that begin with the letters log:



objFSO.CopyFolder "C:\Scripts\Log*" ,  "C:\Archive",  True

When the preceding line of code is run, the folders C:\Scripts\Logs and C:\Scripts\Logfiles are copied, along with all the files stored within those folders. However, the files within the C:\Scripts folder are not copied.

When you use the CopyFolder method, you cannot copy only the files in a folder without also copying the files in any subfolders. To copy only the files and not the subfolders, use the CopyFile method instead. (This method is discussed later in this chapter.)

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft