delete file using wmi

  • Thread starter Thread starter Rafael T
  • Start date Start date
R

Rafael T

Hello

I'm trying to delete files and folders from the temp folder however if I use
the FSO I get an insufficient rights error

found out at MS site a script to delete the folders inside a folder using
WMI which wprks well but doesn't delete the files

any ideas?
RT
 
Hi Rafail,

Rafael T said:
I'm trying to delete files and folders from the temp folder however if I use
the FSO I get an insufficient rights error
for working with WMI you need also many rights.
found out at MS site a script to delete the folders inside a folder using
WMI which wprks well but doesn't delete the files
Are you sure that the files inside this folders will be also deleted?

Which script? Post your code, only then we can help you.

Bye
Peter
 
Peter,

Here is teh code I found.

Dim arrFolders()
intSize = 0

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "C:\Documents and Settings\USER\Local Settings\Temp"

Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1

For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next

Sub GetSubFolders(strFolderName)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
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"

it goes to the temp folder and deletes all the subdirectories with its
files, but it doesn't delete the files from the temp folder.
RT
 
Back
Top