Solved

How to create a file list to list all the, let's say *.txt files, in a driver?

Posted on 1998-07-03
8
142 Views
Last Modified: 2010-05-03
I need to create a list of files to list all the files in a directory (or a driver).  These files follow certain patterns (let's say *.frm, *.bas,  ....) and may be in the subdirectories, or sus-subdirectories. Then I will process each file accordingly.
0
Comment
Question by:Jingya
[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
8 Comments
 
LVL 2

Expert Comment

by:peterwest
ID: 1464896
Hi there,

I'm not sure if you want the programme to automatically search down through the subdirectories or not.  The following code will print all of the files ending .txt in the current directory to the screen, although you could modify the *.txt property to include a directory path as well....


'Declare local variables
Dim sFileName As String

'Get the first file that matches the file mask
sFileName = Dir$("*.txt")

'Loop util no more matches are found
Do While sFileName <> ""
   
    'Display the filename
    Me.Print sFileName
   
    'Get the next file that matches the criteria
    sFileName = Dir$

Loop


Hope this helps

Pete

0
 

Expert Comment

by:qhenry
ID: 1464897
I would use a file list box with a .pattern property of '*.txt' etc. Now you can individually process each item in the list box.
qhenry
0
 
LVL 2

Expert Comment

by:peterwest
ID: 1464898
Hi again,

Another method you could use would be to use the FindFirstFile and FindNextFile API calls; these allow you to retrieve extended information relating to the files too - including file size, date etc. etc.

Pete
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Jingya
ID: 1464899
I appreciate the answers. But these answers are too simple and do not answer the key point -- the list should include all the qualified files in all the sub- and sub-sub- directories. This is the key point!!  I want a smart way to do it.  I don't think this question worths more than 50 point. All we need is to list the herach of subdirectories and then dir each directory.  Principlely this is not difficult but tedious. I wonder whether we could do this  in a smart way.
0
 

Author Comment

by:Jingya
ID: 1464900
Further comment:  Let me make the question more clear.  I want a code automatically search and find the sub- sub-sub- directory structure in an assigned directory or disk drive, then search each directory to find files ended with .txt and list the file name in a file name array for further processing.
0
 
LVL 2

Expert Comment

by:peterwest
ID: 1464901
So, just to quantify one more time - you want a method that will allow you to start searching, say, in the root directory and have the search performed over all sub-directories from that point.

Pete

0
 

Author Comment

by:Jingya
ID: 1464902
Pete:
     Exactly as you discribed! Thank you to help me clarify the question.

Jingya
0
 
LVL 2

Accepted Solution

by:
swilt earned 50 total points
ID: 1464903
This will search C:\ with subdirectories for either a specific file or pattern entered into a textbox (Text1) when a command button is pressed (Command1)

    Option Explicit

    Dim colFiles As Collection

    Private Sub Command1_Click()
        Dim vIte As Variant
         
        Set colFiles = New Collection
        MyFindFiles "C:\", Text1.Text
        For Each vIte In colFiles
            MsgBox vIte
        Next vIte
        Set colFiles = Nothing
         
    End Sub

    Private Sub MyFindFiles(ByVal sPath As String, ByVal sFilePattern As String)
        Dim sFile As String
        Dim vIte As Variant
        Dim colDir As New Collection
         
        On Error Resume Next
         
        sFile = Dir$(sPath & sFilePattern)
        While sFile <> ""
            If Left$(sFile, 1) <> "." Then
                colFiles.Add sPath & sFile
            End If
            sFile = Dir$
        Wend
             
        sFile = Dir$(sPath, vbDirectory)
        While sFile <> ""
            If Left$(sFile, 1) <> "." Then
                If GetAttr(sPath & sFile) And vbDirectory Then
                    colDir.Add sPath & sFile & "\"
                End If
            End If
            sFile = Dir$
        Wend
         
        For Each vIte In colDir
            MyFindFiles vIte, sFilePattern
        Next vIte
         
        Set colDir = Nothing
    End Sub
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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 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