Export (0) Print
Expand All

WMI Tasks: Files and Folders

WMI tasks for files and folders change file or folder properties through WMI, including creating a share or renaming a file. If you want to copy a file or read and write a file, the easiest way is to use the Windows Script Host FileSystemObject rather than WMI. For other examples, see the Files and Folders section of the TechNet ScriptCenter.

CIM_DataFile is one of the few CIM classes in WMI that is implemented. Avoid enumerating or querying for all instances of CIM_DataFile on a computer because the volume of data is likely to either affect performance or cause the computer to stop responding.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

Aa394594.wedge(en-us,VS.85).gifTo run a script

  1. Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript scriptfile.vbs at the command prompt.

Note  By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I...WMI classes or methods
...rename a file without getting an error message?Use the CIM_DataFile class. Ensure that you pass the entire path name when calling the Rename method, for example, "C:\Scripts\Test.txt" instead of "Text.txt".
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_DataFile where Name = " _
        & "'c:\\scripts\\toggle_service.vbs'")
For Each objFile in colFiles
    errResult = objFile.Rename("c:\scripts\toggle_service.old")
Next
...determine whether users have .MP3 files stored on their computer?Use the CIM_DataFile class and select files using the following WQL WHERE clause: Where Extension = "MP3".
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Extension = 'mp3'")
For Each objFile in colFiles
    Wscript.Echo "File Name: " & objFile.Name & "." & objFile.Extension
    Wscript.Echo "Path: " & objFile.Path
Next
...create shared folders on a computer?Use the Win32_Share class and the Create method.
Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 25
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set objNewShare = objWMIService.Get("Win32_Share")
errReturn = objNewShare.Create("C:\Finance", "FinanceShare", FILE_SHARE, _
        MAXIMUM_CONNECTIONS, "Public share for the Finance group.")
...copy a folder?Use the Win32_Directory class and the Copy method.
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colFolders = objWMIService.ExecQuery( _ 
    "Select * from Win32_Directory where Name = 'c:\\Scripts'") 
 
For Each objFolder in colFolders 
    errResults  = objFolder.Copy("D:\Archive") 
Next 
...move a folder?Use the Win32_Directory class and the Rename method.
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colFolders = objWMIService.ExecQuery _ 
    ("Select * from Win32_Directory where name = 'c:\\Scripts'") 
 
For Each objFolder in colFolders 
    errResults = objFolder.Rename("C:\Admins\Documents\Archive\VBScript") 
Next

 

Related topics

WMI Tasks for Scripts and Applications
WMI C++ Application Examples
TechNet ScriptCenter

 

 

Show:
© 2014 Microsoft