Solved

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

Posted on 2001-07-26
5
1,457 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
Comment Utility
the following command will do that:
dir/s *.txt -> MyTextFile.txt

Regards,
CJ

0
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks

work very fine :)

Falk2
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now