Solved

VBScript retrieve filenames using wildcard

Posted on 2007-03-19
4
758 Views
Last Modified: 2010-10-17
Is there a way in VBScript to retrieve a list of the files in a folder using wildcards?
My script uses the FileSystemObject (fso.GetFolder) but this returns all files in the specified folder.
There are at least a thousand files in the folder but I want to retrieve a subset, similar to DIR AP*.xml.
My kludgy solution is to test each filename ... IF Left(strFilename,2) = "AP" ... but as I have to test so many files this is unacceptably slow.  Note that I am retrieving the folder contents from a mapped drive on a remote server.
0
Comment
Question by:wlevy
  • 2
  • 2
4 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18748056
You can either test the extension in your loop:

for each file in objFolder.Files
  If objFSO.GetExtensionName(file.path) = "xml" Then...

Or use wmi:
strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMI.ExecQuery _
    ("Select * from CIM_Datafile Where Extension = 'xml'")

For Each objFile in colFiles
    Wscript.Echo "Name: " & objFile.Name
Next
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18749081
Did that not work for you?  I noticed you graded this a "B", just curious what could have been added to warrant an "A"?
0
 

Author Comment

by:wlevy
ID: 18749097
By adding more conditions to the WHERE clause I was able to filter the list to include only files in a specific folder that start with "AP" and have an extension of xml.  This runs MUCH faster than using FileSystemObject.

Note that if listing a very large number of files where you want most of them, FileSystemObject is faster.

Thanks for your help!
0
 

Author Comment

by:wlevy
ID: 18749412
Sorry, perhaps I graded too harshly.  I gave a grade of "B" because I had to do a lot of research and experimenting to satisfy all of my requirements (drive, path, filename wildcard, extension).
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question