Solved

VBScript retrieve filenames using wildcard

Posted on 2007-03-19
4
745 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now