On Error Resume Next
strComputer = "."
[green]'Change path below to directory to clean out[/green]
strFolder = "C:\Users\markmac\AppData\Local\Temp"
Dim objWMIService, colFileList, objFile, objFolder, colSubfolders, colSubfolders2
Dim strFolderName, arrFolders()
intSize = 0
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='"& strFolder &"'} Where " _
& "ResultClass = CIM_DataFile")
If colFileList.Count = 0 Then
Wscript.Echo "There were no files in the folder."
Else
For Each objFile In colFileList
objFile.Delete
Next
End If
[green]'Uncomment the next line to prevent deletion of subdirectories.[/green]
[green]'Wscript.Quit[/green]
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolder & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolder
intSize = intSize + 1
For Each objFolder in colSubfolders
GetSubFolders strFolder
Next
Sub GetSubFolders(strFolder)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolder & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
For Each objFolder2 in colSubfolders2
strFolder = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolder
intSize = intSize + 1
GetSubFolders strFolder
Next
End Sub
For i = Ubound(arrFolders) to 0 Step -1
strFolder = arrFolders(i)
strFolder = Replace(strFolder, "\", "\\")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolder & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
Next
msgbox "done"