Solved

VBScript retrieve filenames using wildcard

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting The Dots from Range Value to Listbox ListIndex 11 25
Need help Creating PowerShell Script 4 34
PowerShell logging 1 27
Powershell Split 18 27
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

680 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