Vbscript problem

November 4, 2009 at 01:04:45
Specs: Windows XP \ 2003
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 = Nothing

Any help would be greatly appreciated!

Thanks in advance!

T1b


See More: Vbscript problem

Report •


#1
November 4, 2009 at 05:46:58
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 = Nothing

The 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.


Report •

#2
November 4, 2009 at 18:57:51
or maybe you could apply "instr" function to file.name:

p=instr(ucase(objFile.name),"_AFP_")
if p > 0 then objFile.delete

or 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_")


Report •

#3
November 5, 2009 at 02:44:20
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.

Report •

Related Solutions

#4
November 6, 2009 at 01:13:08
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?")


Report •

#5
November 6, 2009 at 01:16:46
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 objFiles

pdot=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
Next

I just keep getting expected End of Statement error on line 11 char 23

Thank you again for all your replies


Report •

#6
November 6, 2009 at 03:03:01
expected End of Statement

Do 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.


Report •

#7
November 6, 2009 at 03:54:56
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!


Report •

#8
November 6, 2009 at 04:13:23
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


Report •

#9
November 6, 2009 at 22:26:12
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

Report •

#10
November 7, 2009 at 15:43:10
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.


Report •


Ask Question