Search code examples
vbawmiwmi-querywql

VBA code to find all hidden folders in directory with WQL and store it in xml file


I managed to get all the hidden folders now and the only problem is to store all the information about it into the xml file. Can anyone help me with this?

Dim xdoc
Function CreateFolders(objFile)
  Dim elem
  Dim attr
  set elem = xdoc.createElement("Folders")
  set attr = xdoc.createAttribute("Description")
  attr.value = objFile.Description
  elem.setAttributeNode attr
  set CreateFolders = elem
End Function

Dim FilePath

Dim objFile
Dim root
Dim elem, elem1


Set xdoc = CreateObject("MSXML2.DOMDocument.6.0")
xdoc.appendChild xdoc.createProcessingInstruction        ("xml", "version=""1.0""     encoding=""utf-8""")
FilePath = "C:\Users\User\Downloads\M.xml"

set root = xdoc.createElement("Folders")
xdoc.appendChild root


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Hidden = True")
 For Each objFile in colFiles
   Wscript.Echo objFile.Name
Next

Thanks for your time in advance.


Solution

  • Here's the solution that I found. May be it'll help someone in the future

    Dim xdoc
    Function CreateFolders(objFile)
      Dim elem
      Dim attr
      set elem = xdoc.createElement("Folder")
      set attr = xdoc.createAttribute("Description")
      attr.value = objFile.Description
      elem.setAttributeNode attr
      set CreateFolders = elem
    End Function
    
    Dim FilePath
    Dim objFile
    Dim root
    Dim elem, elem1
    
    
    Set xdoc = CreateObject("MSXML2.DOMDocument.6.0")
    xdoc.appendChild xdoc.createProcessingInstruction("xml", "version=""1.0""       encoding=""utf-8""")
    FilePath = "C:\Users\User\Downloads\My.xml"
    
    set root = xdoc.createElement("Folders")
    xdoc.appendChild root
    
    Wscript.Echo "Stage 1 Done"
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from Win32_Directory Where Hidden = True")
    Wscript.Echo "Stage 2 Done"
    
    For Each objFile in colFiles
    set elem = CreateFolders(objFile)
    root.appendChild elem
    
    
    Next
    Wscript.Echo "Stage 3 Done"
    xdoc.Save FilePath