Deleting Folders

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Folders are not necessarily permanent additions to a file system. At some point, folders might need to be deleted, perhaps because they are no longer required, because the role of the computer has changed, or because the folders were created by mistake.

The Win32_Directory class includes a Delete method that allows you to delete folders: you simply bind to the folder in question and then call the Delete method. After the Delete method is called, the folder is permanently removed from the file system; it is not sent to the Recycle Bin. In addition, no confirmation notice ("Are you sure you want to delete this folder?") is issued. Instead, the folder is immediately removed.

You cannot delete read-only folders using the FileSystemObject; however, this can be done using WMI. If your script uses WMI and you do not want to remove a read-only folder, you must use the Readable property to check the folder status before deleting it.

Scripting Steps

Listing 11.14 contains a script that deletes the folder C:\Scripts. 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 Win32_Directory class.

    To limit data retrieval to a specified folder, a Where clause is included restricting the returned folders to those with the name C:\\Scripts. You must include both backslashes (\\) in the name.

  4. For the single folder in the collection, use the Delete method to delete the folder, and then echo the results of this operation. The value 0 indicates that the folder was successfully deleted.

Listing 11.14   Deleting Folders

1 2 3 4 5 6 7 8 9 
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.Delete     Wscript.Echo errResults 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