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!

search and delete files

Status
Not open for further replies.

Hasfurt

IS-IT--Management
Mar 21, 2005
12
US
This is what I have come up with the problem with the code is that it wont go into the sub folders to delete the files with the extensions


Dim fso, counter, sroot, objFSO, x
Dim fld, fls, oFile, dt, oFolder, strWildcardsToDelete
set fso = CreateObject("Scripting.FileSystemObject")
counter = 0
sroot = "C:\test\"
strWildcardsToDelete = "*.txt,*.mp3"

call deleteFile(sroot, strwildcardstodelete)

msgbox(counter)

sub deleteFile(StartPath, strwildcards)
Set fld = fso.getFolder(Startpath)
Set Fls = Fld.Files
Set objFSO = CreateObject("Scripting.FileSystemObject")
for Each oFile in Fls
on error resume next
if err.number=0 then
aExtensions = split(strWildcards, ",")
For i = LBound(aExtensions) to UBound(aExtensions)
'Delete the file
counter = counter + 1
objFSO.DeleteFile(startpath & aExtensions(i))
Next
else
'do something if necessary
err.clear
end if
on error goto 0

for each ofolder in Fld.subfolders
call deleteFile(ofolder.path, x)
msgbox "IM here"
next

next
end sub

i used the VAR X just as a decoy so i dont get the error "not enough parameters"

Thanks for any help
 
Hello Hasfurt!

i used the VAR X just as a decoy so i dont get the error "not enough parameters"
When you call your routine passing "X", your split function will not work as expected. Perhaps you are incorrectly assuming that every time the routine calls itself, strWildcards will still contain "*.txt,*.mp3"?? To make this work, change your call to use your origional wildcard chars. It should work better then.

Good luck!
 
Hello Hasfurt,

I changed your code a bit:
[1] Use Option Explicit at the top of your code to avoid incorrectly typing the name of an existing variable or to avoid confusion in code
[2] disassociate an object variable from any actual object. (set fso = Nothing) Memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing.
[3] Like Snotmare mentioned you have to pass the parameters every time the sub is called. Even in a recursive function.
[4] When using wildcards in the DeleteFile function it is not necessary to do a for loop for each file in the folder. It will remove all the files in the folder with the given extention within the first pass. So I removed that loop.

Code:
Option explicit
Dim counter, sroot, strWildcardsToDelete
Dim fso, fld, oFolder, aExtensions, i

counter = 0
sroot = "H:\leeg\HPInsightDiagnostics\"
strWildcardsToDelete = "*.cab,*.exe"

call deleteFile(sroot, strwildcardstodelete)
msgbox(counter)

sub deleteFile(StartPath, strwildcards)
	set fso = CreateObject("Scripting.FileSystemObject")
	Set fld = fso.getFolder(Startpath)
 	on error resume next
	if err.number=0 then
		aExtensions = split(strWildcards, ",")
		For i = LBound(aExtensions) to UBound(aExtensions)
			'Delete the file
 			counter = counter + 1
			fso.DeleteFile(startpath & aExtensions(i))
		Next
	end if
 	on error goto 0
	
	for each ofolder in Fld.subfolders               
 		msgbox "IM here: " & ofolder.path
		deleteFile ofolder.path, strwildcards
	next
	Set fld = Nothing
	set fso = Nothing
end sub



Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
I tried out your code and their was still a little bit of a bug. It will delete the files at the start level but it wont delete the files at the sub-folder level. Yet the counter counts thos files but just wont delete them.
 
Replace this:
deleteFile ofolder.path, strwildcards
By this:
deleteFile ofolder.path & "\", strwildcards

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Your a genious PHV!! Thanks a lot for all the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top