Solved

VBScript retrieve filenames using wildcard

Posted on 2007-03-19
4
764 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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…

729 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