Copying Files

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

In addition to copying entire folders, you often need to copy individual files or specific sets of files. For example, you might need to copy a particular template to each computer or copy all .vbs files to a new administrator workstation. The CIM_Datafile Copy method can be used to copy individual files or a set of files from one location to another.

This ability to copy individual files (as opposed to entire folders) is especially powerful when combined with WMI s ability to locate all the files of a specified type regardless of their location in the file system. This allows you to perform such tasks as automatically searching for all .log files and copying them to a central monitoring station, or searching for all Microsoft Word files and copying them to a document repository.

You can use the CIM_DataFile class and the Copy method to copy either a single file or a set of files to a new location.

Scripting Steps

Listing 11.25 contains a script that copies all the Microsoft® Windows Media® files on a computer to a specified folder. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  3. Use the ExecQuery method to query the CIM_DataFile class.

    To limit data retrieval to Windows Media files, a Where clause is included restricting the returned files to those with the extension .wma.

  4. For each file in the collection, set the value of the variable strCopy to the new path for the file. This value is a combination of the following items:
    • The destination folder (C:\Media Archive\). You must include the trailing backslash.
    • The file name. If the file is named Annual Meeting.wma, the file name is Annual Meeting.
    • A period (.), used to separate the file name and the extension.
    • The extension.

    For example, if you have a file named D:\Finance\Annual Meeting.wma, the value of strCopy is C:\Media Archive\Annual Meeting.wma.

  5. Use the Copy method to copy the file to the new file path.

Listing 11.25   Copying Files

1 2 3 4 5 6 7 8 9 10 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFiles = objWMIService.ExecQuery _     ("SELECT * FROM CIM_DataFile WHERE Extension = 'wma'") For Each objFile in colFiles     strCopy = "C:\Media Archive\" & objFile.FileName _         & "." & objFile.Extension     objFile.Copy(strCopy) Next

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