Dir /S in DOS - Dir all files in all sub dir's

mr_e_landis

Registered User.
Local time
Today, 02:39
Joined
Feb 16, 2005
Messages
28
I'm working on a "Simple" project but it got a little complicated... As usual :rolleyes:
Its easy in DOS... You just run a Dir /S and viola... All your files including files in sub directories...

So far I've used the MS example code below and it works great!!!
Code:
'''' Display the names in C:\ that represent directories.
'''MyPath = "c:\"    ' Set the path.
'''MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
'''Do While MyName <> ""    ' Start the loop.
'''    ' Ignore the current directory and the encompassing directory.
'''    If MyName <> "." And MyName <> ".." Then
'''        ' Use bitwise comparison to make sure MyName is a directory.
'''        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'''            Debug.Print MyName    ' Display entry only if it
'''        End If    ' it represents a directory.
'''    End If
'''    MyName = Dir    ' Get next entry.
'''Loop

In a nut shell this is what I'm doing... There is a root directory. Every folder in that root directory represents a catalog. Thats what the code above does such a fantastic job of. It returns a wonderful list of all the catalogs in the root directory. Now... I need to collect some information from every file in the catalog directory including any sub directories...

I'd like to do that by using the usual code below in a loop. Thats easy :cool:
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(<location here>)
f.size
f.name
Etc...

The underlying issue is getting all the files... There may be 20 files just hang'in out in the Catalog directory or there may be 20 files total, 2 being in the Catalog directory and the rest scattered throughout various sub directories in the Catalog...

HOW CAN I GET A LIST OF ALL FILES INCLUDING FILES IN ALL SUBDIRECTORIES?

:confused:

I've tried the following but agian. No files from subdirectories included in this...
Code:
    Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.Files
    For Each f1 in fc
        s = s & f1.name 
        s = s &  vbCrLf
    Next
    MsgBox s
 
Last edited:
I think I solved my problem...

FoundFiles Property
Returns a FoundFiles object that contains the names of all the files found during a search. Read-only.

Example
This example steps through the list of files found during a search and displays the path for each file.

Code:
With Application.FileSearch
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next I
End With

NewSearch Method
Some of the content in this topic may not be applicable to some languages.

Resets all the search criteria settings to their default settings.

expression.NewSearch
expression Required. An expression that returns a FileSearch object.

Remarks
Search criteria settings are retained throughout an application session. Use this method every time you change search criteria. This method will not reset the value of the LookIn property.

Example
This example uses the NewSearch method to reset the default search criteria before beginning a new search.

Code:
With Application.FileSearch
    .NewSearch
    .LookIn = "C:\My Documents"
    .SearchSubFolders = True
    .FileName = "run"
    .TextOrProperty = "San*"
    .MatchAllWordForms = True
    .FileType = msoFileTypeAllFiles
    If .Execute() > 0 Then
        MsgBox "There were " & .FoundFiles.Count & _
        " file(s) found."
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
    Else
        MsgBox "There were no files found."
    End If
End With
 
Last edited:
THIS IS IT! The exact code I needed! It needs some fine tuning for me but it provides exactly what I needed!!!

If you ever get stuck just create a thread asking for help and you'll find the answer on your own soon after...

Code:
With Application.FileSearch
    .NewSearch
    .LookIn = f.Path
    .SearchSubFolders = True
    .FileName = "*.*"
    If .Execute() > 0 Then
        MsgBox "There were " & .FoundFiles.Count & " Files found."
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
    Else
        MsgBox "There were no files found."
    End If
End With
 

Users who are viewing this thread

Back
Top Bottom