Moving a Folder and Its Contents

Microsoft® Windows® 2000 Scripting Guide

When you copy a folder from one location to another, you end up with duplicate copies of the information. Sometimes that is exactly what you want. On other occasions, however, you do not want two copies of the information; instead, you want to move the sole copy from Computer A to Computer B, or from hard disk C to hard disk D.

Moves such as this are often done to free disk space on a particular drive; for example, you might periodically move seldom-accessed folders to an archive drive. Alternatively, you might have a monitoring script that logs information to the local computer. When monitoring is complete, you might want that information uploaded to a central monitoring station and then deleted from the local computer. That way, the local computer will be prepared for the next round of monitoring.

The MoveFolder method accepts two parameters:

  • Source folder (the folder to be moved). This folder can be specified either as a local path or as a UNC path.

  • Destination folder (the location where the folder is to be moved). This folder can be specified either as a local path or as a UNC path.

If the destination folder does not exist, the source folder will be moved. If the destination folder already exists, however, the move operation will fail. You cannot use MoveFolder to overwrite an existing folder.

The script in Listing 4.10 moves the local folder, C:\Scripts, to the shared folder \\helpdesk\management.

Listing 4.10 Moving a Folder


Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\Scripts" , "\\helpdesk\management"

Note that the MoveFolder method cannot perform any sort of rollback should the script fail. For example, suppose a network connection fails before a script has been able to move all the files from one computer to another. In a case such as that, you will end up with some files on Computer A, some files on Computer B, and possibly even a file or two lost in transit. However, there is no way for MoveFolder to roll back the failed transactions and restore the two computers to their previous states.

Because of that, you might want to use two methods, CopyFolder and DeleteFolder, when transferring folders and their contents across the network. You can use CopyFolder to copy the folder from Computer A to Computer B. If the copy operation succeeds, you can then use DeleteFolder to delete the folder on Computer A. If the operation fails, you can cancel the delete command and rest assured that the folder and all its contents are still safely stored on Computer A.