Computing.Net > Forums > Programming > Vbscript problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Vbscript problem

Reply to Message Icon

Name: T1berious
Date: November 4, 2009 at 01:04:45 Pacific
OS: Windows XP \ 2003
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: November 4, 2009 at 05:46:58 Pacific
Reply:

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.


0

Response Number 2
Name: nbrane
Date: November 4, 2009 at 18:57:51 Pacific
Reply:

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


1

Response Number 3
Name: klint
Date: November 5, 2009 at 02:44:20 Pacific
Reply:

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.


0

Response Number 4
Name: nbrane
Date: November 6, 2009 at 01:13:08 Pacific
Reply:

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?")


0

Response Number 5
Name: T1berious
Date: November 6, 2009 at 01:16:46 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: klint
Date: November 6, 2009 at 03:03:01 Pacific
Reply:

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.


0

Response Number 7
Name: T1berious
Date: November 6, 2009 at 03:54:56 Pacific
Reply:

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!


0

Response Number 8
Name: klint
Date: November 6, 2009 at 04:13:23 Pacific
Reply:

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


0

Response Number 9
Name: nbrane
Date: November 6, 2009 at 22:26:12 Pacific
Reply:

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


0

Response Number 10
Name: klint
Date: November 7, 2009 at 15:43:10 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Vbscript problem

VBScript Problem www.computing.net/answers/programming/vbscript-problem/9169.html

Vbscript, problems with Do...loop www.computing.net/answers/programming/vbscript-problems-with-doloop/14274.html

vbScript Looping problem www.computing.net/answers/programming/vbscript-looping-problem/20082.html