Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-07-26
5
Medium Priority
?
1,546 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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

730 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