Deleting Files

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Deleting files is as an integral part of file management. Files need to be deleted for many reasons: to free up disk space; to remove outdated files; to remove inappropriate files. WMI is a powerful tool for deleting files because it allows you to select a specified set of files (regardless of their physical location in the file system) and then delete all those files in a single operation.

For example, you might have a policy that prohibits users from storing media files on a particular file server. A WMI script can be scheduled to run each night, search for a specific set of files (for example, .mp3 or .wma files), and then delete all instances of that file type.

Unlike the FileSystemObject, the WMI Delete method automatically deletes read-only files. If you do not want to delete read-only files, you have to construct your query in such a way as to exclude these files from the returned collection (for example, by including the clause Where Readable = True).

Scripting Steps

Listing 11.26 contains a script that deletes all the Windows Media files from a computer. 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, use the Delete method to delete the file.

Listing 11.26   Deleting Files

1 2 3 4 5 6 7 8 
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     objFile.Delete 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