Solved

VBScript retrieve filenames using wildcard

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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