Changing File Name Extensions

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Using a script to rename a single file is inefficient; usually, it is faster to carry out this task using Windows Explorer or a command-line tool. A more practical operation might be to use a script to change the extensions of all the files in a folder. For example, you might have a folder containing numerous log files on a computer, all with the extension .log. Using a script, you can change the extension for all of these .log files to .txt or .bkp or anything else. These repetitive tasks represent the kinds of things that scripts do best.

When changing file name extensions using WMI, it is important to keep in mind that the Rename method requires a complete path. This means that you cannot simply specify a new file name extension for the file. Instead, you must construct an entire path.

For example, suppose you have a file named Performance.log in the C:\Logs\MailServer folder. To rename this file to Performance.bkp, you need to pass C:\Logs\MailServer\Performance.bkp as the Rename parameter. This new path can be constructed by combining:

  • The CIM_Datafile Drive property (C:).
  • The Cim_Datafile Path property (\Logs\MailServer\).
  • The CIM_Datafile FileName property (Performance).
  • A dot (.) to separate the file name and extension.
  • Bkp, the new extension.

Scripting Steps

Listing 11.24 contains a script that changes the file name extensions of all the .log files in a specified folder to .txt. 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 and an Associators of query to return a collection of all the files within the folder C:\Scripts. The Associators of query requires the following parameters:
    • The source class, Win32_Directory, where Name equals C:\Scripts.
    • The ResultClass, CIM_Datafile. This limits data retrieval to files, which are instances of the CIM_Datafile class. Subfolders are not included in the returned collection.
  4. For each file in the collection, check the file name extension.
  5. For each file in the collection with the file name extension .log, create a new file name using the following:
    • The folder path (C:\Scripts\).
    • The existing file name. (For example, for the file C:\Scripts\DiskPerformance.log, the FileName is DiskPerformance.)
    • A dot (used to separate the FileName from the file name extension).
    • txt (the new file name extension).

    Consequently, the file C:\Scripts\DiskPerformance.log is given a new path: C:\Scripts\DiskPerformance.txt.

  6. Use the Rename method to rename the file, and then echo the results of that procedure. A 0 indicates that the file was successfully renamed.

Listing 11.24   Changing File Name Extensions

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set FileList = objWMIService.ExecQuery _     ("ASSOCIATORS OF {Win32_Directory.Name='c:\Scripts'} Where " _         & "ResultClass = CIM_DataFile") For Each objFile In FileList     If objFile.Extension = "log" Then         strNewName = objFile.Drive & objFile.Path & _             objFile.FileName & "." & "txt"         errResult = objFile.Rename(strNewName)         Wscript.Echo errResult     End If 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