Solved

Dir Function -> get all *.TXT in a folder and there subfolders

Posted on 2001-07-26
5
1,477 Views
Last Modified: 2009-03-06
Hi!

is there an easy way to get all *.txt in a folder (e.g. C:\Programs) and then get from this folder all subfolders with have *.txt with the function DIR?

Falk2
0
Comment
Question by:Falk2
5 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6321467
the following command will do that:
dir/s *.txt -> MyTextFile.txt

Regards,
CJ

0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6321511
Uhm, should've also said:
it will write the contents to mytextfile.txt

regards,
CJ
0
 
LVL 5

Accepted Solution

by:
GeoffKell earned 100 total points
ID: 6321646
I assumed that you meant the VB DIR function rather than the DOS command so here's an example which gathers all the folders/sub-folders into a collection then traverses this getting a list of all the *.TXT files into another collection which it then returns

Regards
GK

Option Explicit

Private Sub Form_Load()

    Dim v As Variant
    Dim c As Collection
   
    Set c = GetTxtFiles("C:\Program Files\")
   
    For Each v In c
        Debug.Print v
    Next v
    Debug.Print c.Count

End Sub

Function GetTxtFiles(ByVal strPath As String) As Collection

    Dim i As Integer
    Dim strItem As String
    Dim strParent As String

    Dim colFiles As Collection
    Dim colFolders As Collection

    Set colFiles = New Collection
    Set colFolders = New Collection
   
    ' Find all the folders
    colFolders.Add TidyDir(strPath)

    i = 1
    Do Until i > colFolders.Count
        strParent = colFolders.Item(i)
        strItem = Dir(strParent, vbDirectory)
        Do Until Len(strItem) = 0
           
            ' Make sure it's a directory
            If (GetAttr(strParent & strItem) And vbDirectory) = vbDirectory Then
                If strItem <> "." And strItem <> ".." Then
                    colFolders.Add TidyDir(strParent & strItem)
                End If
            End If
            strItem = Dir
        Loop
        i = i + 1
    Loop


    ' Now loop through the folders getting all the files
    i = 1
    Do Until i > colFolders.Count
        strItem = Dir(colFolders.Item(i) & "*.txt", vbNormal)
        Do Until Len(strItem) = 0
            colFiles.Add colFolders.Item(i) & strItem
            strItem = Dir
        Loop
        i = i + 1
    Loop

    Set GetTxtFiles = colFiles

End Function

Private Function TidyDir(ByVal strDir As String) As String

    TidyDir = IIf(Right$(strDir, 1) = "\", strDir, strDir & "\")

End Function
0
 
LVL 3

Expert Comment

by:RodStephens
ID: 6321810
For an example that searches subdirectories for files matching a pattern (*.txt, etc.), look at:

    http://www.vb-helper.com/howto/search.zip

For an example that does the same thing but uses the API instead of VB's Dir function, look at:

    http://www.vb-helper.com/howto/search.zip

This version is faster but more complicated.

For some other interesting examples, go to:

    http://vb.digitalroutes.co.uk/index.shtml?file+search

These examples do things like search subdirectories for files that contain a certain string or search for .doc files that contain a certain string.
0
 

Author Comment

by:Falk2
ID: 6321924
thanks

work very fine :)

Falk2
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

775 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