Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 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…
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…

867 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

12 Experts available now in Live!

Get 1:1 Help Now