Solved

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

Posted on 2001-07-26
5
1,510 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
[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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 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