Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm trying to code a script that will search through a folder and subfolders and delete by filname rather than extension.
Dim objFSO, objFolder, objFiles, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Test")
Set colSubfolders = objFolder.Subfolders
Set objFiles = objFolder.Files
For Each objFile in objFiles
If UCase(left(objFile.Name,6)) = "*_AFP_*" Then objFile.Delete, True
Next
Set objFiles = Nothing
Set objFolder = Nothing
Set objFSO = NothingAny help would be greatly appreciated!
Thanks in advance!
T1b

If UCase(left(objFile.Name,6)) = "*_AFP_*"The above condition will never be true. There are no files actually named "*_AFP_*". They will all have individual names. The thing will have in common will be that they will contain "_AFP_" between characters 2 and 6. So why not say that:
If UCase(mid(objFile.Name,2,5)) = "_AFP_"
(not sure if I've got the numbers correct, but experiment and see.)
Set objFiles = Nothing Set objFolder = Nothing Set objFSO = NothingThe above three statements are completely unnecessary since these objects are about to go out of scope anyway. I wouldn't bother setting any of these to nothing.

or maybe you could apply "instr" function to file.name:
p=instr(ucase(objFile.name),"_AFP_")
if p > 0 then objFile.deleteor to make it more adaptable (in case the target string
is less than 4 and you want to make sure the extension
is not included in the string-test:)pdot=instr(objFile.name,".")
if pdot=0 then pdot=len(objFile.name)
test=ucase(left(objFile.name,pdot-1))
p=instr(test,"_AFP_")

Thanks nbrane, your solution is better than the one I suggested. I was wondering, though, isn't there a method that enumerates a set of files matching a given wildcard spec? That would be better than enumerating all files and then testing each one individually using string comparisons.

klint: thanks for v.o.c, appreciated.
Your suggestion is first thing i tried in various ways and i could not get a collection based on wildcards. I would think that vbs would have a method for this however. i tried
for *_AFP_* in folder.files
for "*_AFP_*" in folder.files
for each folder in folder.files "*_AFP_*"
for each folder in folder.files(*_AFP_*)
and maybe some other variations. I almost know there's a real simple way for vbs to do this, but i'm not uptospeed on vbs. i know as well when someone posts a solution that matches this situat. i will be doing the "palm-to-forhead" maneuver. that's why i like reading these forums.
(me: "duh! why dint i think o that?")

Hi guy's
Thanks for the replies :) I'm going to have to admit I'm a bit out of my depth (complete novice) and haven't managed to get the script to actually work.
Dim objFSO, objFolder, objFiles, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Test")
Set colSubfolders = objFolder.Subfolders
Set objFiles = objFolder.Files
For Each objFile in objFilespdot=instr(objFile.name,".")
if pdot=0 then pdot=len(objFile.name)
test=ucase(left(objFile.name,pdot-1))
p=instr(test,"_AFP_") then objFile.Delete, True
End If
NextI just keep getting expected End of Statement error on line 11 char 23
Thank you again for all your replies

expected End of StatementDo you mean UNexpected End IF statement?
You seem to have inadvertently deleted a newline and an IF... in the line that has a THEN all by itself. See post 2.

Yes, your right :) Thank you very very much!
It's now deleting the files :)
My last question, how do i get it to transverse through the sub folders?
Again my most heart felt thanks!

Traversing subfolders... there's a whole thread going on right now in this same forum about this subject.
Use this recursive function as a building block and work from there:
Set FSO = CreateObject("Scripting.FileSystemObject") ShowSubfolders FSO.GetFolder(folder_name) Sub ShowSubFolders(Folder) For Each Subfolder in Folder.SubFolders Wscript.Echo Subfolder.Path ShowSubFolders Subfolder Next End Sub

hello klint, while on subject... how can you tell vbs to get hidden/system files and dir.s? I tried variations and got nowhere. not often this feature needed, but if you got quick answer, i would be enlightened. thx

Sorry don't know, I'm not a VBScript expert and only get my information from MSDN:
http://msdn.microsoft.com/en-us/lib...
From the above, I couldn't figure out how to do it.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |