Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using filesystemobject to return files matching a pattern

Status
Not open for further replies.

earme

Programmer
Jul 27, 2000
155
US
Hello all,

I want to know all of the files in a directory that follow the pattern xxxxxx001.ext. I know that I need to use the Folder object to open the folder and return all the files in it, but is there a way to return only the files that match my pattern? or, in my script, do I have to get the name of every file and manually compare it to the pattern(which I don't want to do unless absolutely necessary)?
Any thoughts are appreciated!

Thank you,
Earme
 
Probably the easiest way is when you iterate through the directory, write If-Else statement. This is a small script to list every folder in the C:\InetPub folder. Obviously you can change it to whatever folder you want, or make it dynamic.


<% @ Language=VBScript %>
<%
set fso = Server.CreateObject(&quot;scripting.FileSystemObject&quot;)
set fold = fso.GetFolder(&quot;c:\Inetpub&quot;)

For each folder in fold.subfolders
If right(folder.name,7) = &quot;001.ext&quot; Then
Response.Write(folder.name & &quot;<br>&quot;)
End If
next

Set fold = Nothing
Set fso = Nothing
%>


Hope this helps. -Ovatvvon :-Q
 
Well, that kind of helps. I know the folder that I'm looking in, what I'm looking for are the files in the folder that match a specific pattern. More specifically, is there a way to apply a filter to the object so it only returns the files that match the pattern.

Earme
 
If you want files instead of folders then...in stead of writing:For each folder in fold.subfolders...you'd write the following:


Set files = fold.Files

For each item in files
If right(item.name,7) = &quot;001.ext&quot; Then
Response.write(item.name & &quot;<BR>&quot;)
End If
Next



So to set it to iterate through your current folder and list all of your folders, and all of your items that end in that string specified, do this...


<% @ Language=VBScript %>
<%
set fso = Server.CreateObject(&quot;scripting.FileSystemObject&quot;)
set fold = fso.GetFolder(&quot;c:\Inetpub\asp24h&quot;)
set files = fold.Files

For each folder in fold.subfolders
Response.Write(&quot;<font color='blue'><B>&quot; & UCASE(folder.name) & &quot;</B></font><br>&quot;)
next

Response.write(&quot;<ul>&quot;)
For each item in files
If right(item.name,7) = &quot;001.ext&quot; Then
Response.write(&quot;<li><font color='red'>&quot; & item.name & &quot;</font><BR>&quot;)
End If
Next
Response.write(&quot;</ul>&quot;)

Set files = Nothing
Set fold = Nothing
Set fso = Nothing
%>


This will put all of your folders in Bold Blue print. And all of your files that match the string you specified will be in red and in an unordered list.

You can obviously tweak the code to your likeing.

Hope this clarifies the issue and helps!! :)




-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top