Moving a File

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Instead of copying a file, you might want to move it. For example, if a disk is running low on space, you might want to move a file to a new location. If a computer is changing roles, you might want to move certain diagnostic tools to its replacement. In either case, you do not want two or more copies of the file; you want one copy of the file, stored in a new place.

The MoveFile method enables you to move a file from one location to another. The MoveFile method works exactly like the CopyFile method: You create an instance of the FileSystemObject, call the MoveFile method, and pass two parameters:

  • The complete path to the file to be moved.
  • The complete path to the new location, making sure to include the trailing backslash.

For example, the script in Listing 4.24 moves C:\FSO\ScriptLog.log to the Archive folder on drive D.

Listing 4.24   Moving a File

1 2 
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.MoveFile "C:\FSO\ScriptLog.log" , "D:\Archive\"

Moving a Set of Files

You can also use wildcard characters to move multiple files in a single operation. For example, to move all the files in the FSO folder that begin with the letters data, use the following parameter:

C:\FSO\Data*.*

Wildcard characters are especially useful for moving all the files of a particular type because file types are usually denoted by file name extensions. For example, the script in Listing 4.25 moves all the log files (with the .log file name extension) from the FSO folder on drive C to the Archive folder on drive D.

Listing 4.25   Moving a Set of Files

1 2 
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.MoveFile "C:\FSO\*.log" , "D:\Archive\"

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