Link to home
Start Free TrialLog in
Avatar of mhyohe
mhyohe

asked on

File Searches

I need to search several directories, most having MANY sub-directories themselves, for a particular user-supplied file name.  I am familiar with the DIR function and know I could accomplish my task using it, but it would require a lot of juggling since you can't use the DIR function within a DIR loop.  Is there some way to provide a starting path and a file name, and have it traverse the sub-directories?
Avatar of mhyohe
mhyohe

ASKER

Edited text of question.
Microsoft's article wich can easily be modified to find a specific file:

"HOWTO: Search Directories to Find or List Files"

http://support.microsoft.com/support/kb/articles/Q185/4/76.ASP
Another one:

FindFile - Fast using the Windows API

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=1446
You can use the filesystemobject as well.

   Sub FilesSearch(DrivePath As String, Ext As String)


        Dim XDir() As String
        Dim TmpDir As String
        Dim FFound As String
        Dim DirCount As Integer
        Dim X As Integer
        'Initialises Variables
        DirCount = 0
        ReDim XDir(0) As String
        XDir(DirCount) = ""
        If Right(DrivePath, 1) <> "\" Then
            DrivePath = DrivePath & "\"
        End If
        'Enter here the code for showing the path being
        'search. Example: Form1.label2 = DrivePath
        'Search for all directories and store in the
        'XDir() variable
        DoEvents
            TmpDir = Dir(DrivePath, vbDirectory)
            Do While TmpDir <> ""
                On Error GoTo o
                If TmpDir <> "." And TmpDir <> ".." Then
                    If (GetAttr(DrivePath & TmpDir) And vbDirectory) = vbDirectory Then
                        XDir(DirCount) = DrivePath & TmpDir & "\"
                        DirCount = DirCount + 1
                        ReDim Preserve XDir(DirCount) As String
                    End If
                End If
                TmpDir = Dir
            Loop
            'Searches for the files given by extension Ext
            FFound = Dir(DrivePath & Ext)
            Do Until FFound = ""
                'Code in here for the actions of the files found.
                'Files found stored in the variable FFound.
                'Example: Form1.list1.AddItem DrivePath & FFound
                List1.AddItem DrivePath & FFound
                FFound = Dir
            Loop
            'Recursive searches through all sub directories
            For X = 0 To (UBound(XDir) - 1)
            If blnokay = True Then FilesSearch XDir(X), Ext
            Next X
Exit Sub
o:
Resume Next
        End Sub
Of course you can cheat and shell to DOS momentarily...

SHELL( Environ("Comspec") & " /C DIR C:\" & FileName & " /S /B /-P /ON > C:\Result.Txt"), 1

Actually you'ld have to use ExecCmd instead of Shell to wait for the command to complete. But this is an easy 1 line solution...

M
 
ASKER CERTIFIED SOLUTION
Avatar of bja1
bja1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bjai:

The original source for your code can be found at the MS site which I listed in my first comment.