Using WMI to delete Files or Folders

Using WMI in DesktopX

Just two simple examples:

1. Delete a File:

   Enter the path to your file here:

   file = "C:\Users\Vadim\AppData\Local\Stardock\SD Gadgets\RSS Reader\Feeds\N4G.com"
 
  Set objWMI = GetObject("winmgmts:\\" & "." & "\root\cimv2")
  Set objFIL = objWMI.ExecQuery("ASSOCIATORS OF {Win32_Directory.Name='"& file &"'} Where "&"ResultClass = CIM_DataFile")
   For Each item In objFIL
    item.Delete
   Next
  Set objFIL = nothing : Set objWMI = nothing

1. Delete a Folder:

  Enter the path to your folder here:

   folder = "C:\Users\Vadim\AppData\Local\Stardock\SD Gadgets\RSS Reader\Feeds"
   folder = Replace(folder, "\", "\\") '<== it's need to replace the path to foler...
 
  Set objWMI = GetObject("winmgmts:\\" & "." & "\root\cimv2")
  Set objFOL = objWMI.ExecQuery ("Select * from Win32_Directory where Name = '" & folder & "'")
   For Each item In objFOL
    item.Delete
   Next
  Set objFOL = nothing : Set objWMI = nothing

Long ago I have used FSO for this. But it seems to me that WMI work more quickly.

Best Regards.

12,412 views 4 replies
Reply #1 Top
Nice. :CONGRAT:

I found the ShellApp with dialog's disabled is quicker than FSO also. How's WMI speed on folder collections?
Reply #3 Top
How's WMI speed on folder collections

I didn't measure it specially. However I can see that the speed is very, very high.

Recently I have tested a several ways to search files in a computer (I worked on a tool for global searching for media information and used FSO, WSH and WMI). May tell you that nothing works so quickly as WMI! This is FANTASTIC!  :CONGRAT: 

I guess I'm stupid, what the hell is WMI?

WMI is the Windows Management Instrumentation: About WMI
+1 Loading…