Export (0) Print
Expand All

Copying a File

Microsoft® Windows® 2000 Scripting Guide

Copying files, either from one folder to another on a single computer or from one computer to another, is a common administrative task. For example, you might want to copy a new monitoring script to all your servers or replace an outdated DLL with a newer version. The CopyFile method provides a way to perform these tasks programmatically.

The CopyFile method has two required parameters and one optional parameter:

  • Source (required). Path to the file being copied. This can be either a path on the local computer or a UNC path to a remote computer.

  • Destination (required). Path to the location where the file is to be copied. This can also be a local path or a UNC path.

    To specify that the file keep the same name in its destination location, put a trailing backslash after the destination folder:

    objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\"

    To give the file a new name in its destination location, specify a full file name as the destination:

    objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\NewFileName.txt"

    If the destination folder does not exist, it will automatically be created.

  • Overwrite (optional). By default, the CopyFile method will not copy a file if a file by that same name exists in the destination location. This can be a problem; among other things, this prevents you from replacing an older version of a file with a newer version. To allow the CopyFile method to copy over existing files, set the optional Overwrite parameter to True.

The script in Listing 4.22 copies C:\FSO\ScriptLog.txt to the folder D:\Archive. This operation results in the existence of two files:

  • The original file, C:\FSO\ScriptLog.txt.

  • The copied file, D:\Archive\ScriptLog.txt.

To ensure that the procedure will be carried out even if D:\Archive\ScriptLog.txt exists, the Overwrite parameter is set to True by using the constant OverWriteExisting.

Listing 4.22 Copying a File


Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\", OverwriteExisting

When specifying the destination folder, it is important to include the trailing backslash (for example, D:\Archive\). If the backslash is there, CopyFile will copy the file into the Archive folder. If the backslash is not there, CopyFile will try to create a new file named D:\Archive. If the folder D:\Archive already exists, a "Permission denied error" will be generated, and the copy procedure will fail.

The CopyFile method will also fail if you attempt to overwrite an existing read-only file, even if you have set the OverWrite parameter to True. To copy over a read-only file, you must first delete the file and then call the CopyFile method.

Copying a Set of Files

Wildcard characters provide a way to copy an entire set of files as long as these files are all in the same folder. You can copy a set of files using the same parameters used to copy a single file, but you must include a wildcard as part of the source parameter. For example, the script in Listing 4.23 copies all the .txt files found in C:\FSO to D:\Archive.

Listing 4.23 Copying a Set of Files


Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" , OverwriteExisting

Using wildcards with the CopyFile method allows you to copy all the files in a folder without copying any subfolders in that folder; the CopyFolder method, by contrast, copies both files and subfolders. The following code statement copies all the files in the C:\FSO folder without copying any subfolders:

objFSO.CopyFile "C:\FSO\*.*" , "D:\Archive\"

© 2016 Microsoft