Using WMI in DesktopX
Published on July 7, 2008 By Vad_M In DesktopX Tutorials

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.


Comments
on Jul 07, 2008
Nice.

I found the ShellApp with dialog's disabled is quicker than FSO also. How's WMI speed on folder collections?
on Jul 07, 2008
I guess I'm stupid, what the hell is WMI?
on Jul 08, 2008
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!   

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

WMI is the Windows Management Instrumentation: About WMI
on Jul 09, 2008
I will delete my folders manually.