Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help deleting files in subfolders

Status
Not open for further replies.

Ferg2

MIS
Dec 12, 2006
5
US
Hello all, I have a script that looks for any file with a certain extention and deletes it. The problem I have is it will not look in the subfolders of the folder I have specified. I understand this is a difficult request so I am asking for help. I have pasted what I have so far below. Any assistance would help a ton.

Thx

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

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='D:\'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile in colFiles
If objFile.Extension = "pfc" Then
'strCopy = "D:\Operation Logs\" & objFile.FileName _
'& "." & objFile.Extension
'objFile.Copy(strCopy)
objFile.Delete
End If
Next
 
with the wmi approach is an interesting one,,,if a little slow sometimes :)

if your wmi query only returning files which are in the root of D?
 
Yes that is correct. It will only delete the files under the root of D. I would like to get it to delete the files on the root as well as the files with that extension in any subfolders of D.
 
so you will need to change your WMI query string, can you do an associators of based on Drive letter rather than folder?
there is always the recursive FSO folder searches but these are criminally slow,
i usually wouldnt talk about command line stuff but i dont know your motives but i am sure someone could provide a one line comnmand in cmd to accomplish the task
 
It does not have to be vbs, I can use a cmd if someone can provide. My motive is pretty simple, there are 3 file types that need to be deleted from a bunch of systems and I was looking for a script to accomplish this task even though I am pretty much a novice when it comes to scripting.
 
I have gotten a little further but I am still having problems. Now I can get it to look in the subfolders but it only deletes the files in the final subfolder not the ones leading up to it. here is an example:

d:\temp - it does not delete the files here
d:\temp1 - it does not delete the files here
d:\temp2 - it DOES delete the files from here

Here is the code I have so far if anyone can tell me what I am doing wrong. (sorry about all the lines commented out)

'on error resume next

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

strFolderName = "d:\temp"

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

'Wscript.Echo strFolderName

arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"

Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles
'Wscript.Echo objFile.Name
Next

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
'Wscript.Echo
'Wscript.Echo objFolder2.Name
arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"

Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles
' Wscript.Echo objFile.Name
Next

GetSubFolders strFolderName
Next
For Each objFile in colFiles
If objFile.Extension = "p12" Then
'strCopy = "D:\Operation Logs\" & objFile.FileName _
'& "." & objFile.Extension
'objFile.Copy(strCopy)
objFile.Delete
End If
If objFile.Extension = "pfx" Then
'strCopy = "D:\Operation Logs\" & objFile.FileName _
'& "." & objFile.Extension
'objFile.Copy(strCopy)
objFile.Delete
End If
If objFile.Extension = "pfc" Then
'strCopy = "D:\Operation Logs\" & objFile.FileName _
'& "." & objFile.Extension
'objFile.Copy(strCopy)
objFile.Delete
End If
Next

End Sub
 
strDir = "f:\"
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)

Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
'wscript.Echo aItem.Name
If LCase(Right(Cstr(aItem.Name), 3)) = "bak" Then
'do file manip, copy delete here
End If
Next

For Each aItem In pCurrentDir.SubFolders
'wscript.Echo aItem.Name & " passing recursively"
getInfo(aItem)
Next

End Sub
 
A oneline CMD:
DEL /S/F D:\*.pfc

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you both!!

I can't believe I made something so simple so difficult.
 
Folks...
I'm thinkin of a way to handle this..but always getting stucked...it relates to most of the posts above...but what I want to do is to keep some subfolders and delete the others..say...in c:\Temp.....

I have certain files and subfolders..say the temp folder has the following subfolders..bed, roof, floor & window...I've figured a way to delete the files...but I want to keep,say..."bed" and "roof"....and delete "floor" and "window"...

Can some1 help out here?...
Thx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top