Results 1 to 6 of 6

Thread: [RESOLVED] Search for *.txt

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [RESOLVED] Search for *.txt

    Hello guys.

    I want to make a program that lists all txt files in a certain drive (lets say c:\).
    To do this, do I have to enumerate all directories existing in my drive and search for *.txt in each directory, or vb6 has any feature to do this?

    Thank u very much.
    Last edited by RS_Arm; Nov 6th, 2007 at 05:17 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Search for *.txt

    you can do this using dir, api or fso
    do a search on recursive findfile
    i and others have posted code to do what you want
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: Search for *.txt

    I guess this must be the code, but in line 153, the var " Number_Of_Directories" is not declared. I think that may be here a little bug. Could you help me to fix it?

    Thank u

    VB Code:
    1. Option Explicit
    2.  
    3. Dim Directory_List() As String
    4.  
    5. Dim Total_Number_Of_Directories As Double
    6.  
    7. Dim Current_Directory As Double
    8.  
    9. Private Function Get_Available_Drives(Drive_List() As String) As Long
    10.    
    11.     On Error Resume Next
    12.    
    13.     Dim Current_Drive As Long
    14.    
    15.     Dim File_System_Object As Object
    16.    
    17.     Set File_System_Object = CreateObject("Scripting.FileSystemObject")
    18.    
    19.     Dim Drive As Object, Computer As Object
    20.    
    21.     Set Computer = File_System_Object.Drives
    22.    
    23.     For Each Drive In Computer
    24.        
    25.         'If it's not a disk drive and if it's a removable
    26.         'hard drive or a fixed drive... I needed
    27.         'Drive.DriveType <> 1 so the computer doesn't
    28.         'load the disk drive on the part where it says
    29.         'Drive.IsReady. Dead give away somethings going
    30.         'on.
    31.        
    32.             If Drive.DriveType <> 1 And (Drive.DriveType = 2 Or Drive.DriveType = 3) Then
    33.                
    34.                 'Is the drive ready?
    35.                
    36.                     If Drive.IsReady Then
    37.                        
    38.                         Current_Drive = Current_Drive + 1
    39.                        
    40.                         ReDim Preserve Drive_List(Current_Drive) As String
    41.                        
    42.                         Drive_List(Current_Drive) = Drive.Path & "\"
    43.                
    44.                     End If
    45.            
    46.             End If
    47.        
    48.     Next
    49.    
    50.     Get_Available_Drives = Current_Drive
    51.    
    52.     'If no drive exists then terminate.
    53.        
    54.         If Current_Drive = 0 Then End
    55.  
    56. End Function
    57.  
    58. Private Sub Recurse_Directory_List(Directory_Path As String)
    59.    
    60.     On Error Resume Next
    61.    
    62.     Dim File_System_Object As Object
    63.    
    64.     Dim Directory As Object
    65.    
    66.     Dim Get_Directory_Path As Object
    67.    
    68.     Dim Sub_Directory As Object
    69.    
    70.     Set File_System_Object = CreateObject("Scripting.FileSystemObject")
    71.    
    72.     Set Get_Directory_Path = File_System_Object.GetFolder(Directory_Path)
    73.    
    74.     Set Sub_Directory = Get_Directory_Path.Subfolders
    75.  
    76.     For Each Directory In Sub_Directory
    77.    
    78.         DoEvents
    79.        
    80.         Current_Directory = Current_Directory + 1
    81.  
    82.         ReDim Preserve Directory_List(Current_Directory) As String
    83.    
    84.         Directory_List(Current_Directory) = Directory & "\"
    85.        
    86.         Recurse_Directory_List (Directory)
    87.    
    88.     Next
    89.    
    90. End Sub
    91.  
    92. Private Sub Create_Directory_List(Drive As String, Directory_List() As String)
    93.    
    94.     On Error Resume Next
    95.    
    96.     ReDim Directory_List(0) As String
    97.    
    98.     Current_Directory = Current_Directory + 1
    99.  
    100.     ReDim Preserve Directory_List(Current_Directory) As String
    101.    
    102.     Directory_List(Current_Directory) = Drive
    103.    
    104.     Recurse_Directory_List Drive
    105.    
    106.     Total_Number_Of_Directories = Current_Directory
    107.    
    108.     Current_Directory = 0
    109.    
    110. End Sub
    111.  
    112. Public Sub Get_All_Directories()
    113.  
    114.     On Error Resume Next
    115.  
    116.     Dim Drive_List() As String
    117.    
    118.     Dim Number_Of_Drives As Long
    119.    
    120.     Dim Current_Drive As Long
    121.    
    122.     Dim File_Path As String
    123.    
    124.     Dim Directory_Path As String
    125.    
    126.     Dim Get_Number_Of_Directories As Double
    127.    
    128.     Dim Current_Sub_Directory As Double
    129.  
    130.     Number_Of_Drives = Get_Available_Drives(Drive_List())
    131.    
    132.     For Current_Drive = 1 To Number_Of_Drives
    133.        
    134.         'The Create_Directory_List will take less than a minute. Has to scan ALL
    135.         'directories in the harddrive.
    136.        
    137.             Create_Directory_List Drive_List(Current_Drive), Directory_List()
    138.        
    139.         For Current_Sub_Directory = 1 To Total_Number_Of_Directories
    140.      
    141.             Do
    142.            
    143.                 DoEvents
    144.                    
    145.                 Current_Directory = Current_Directory + 1
    146.                
    147.                 Directory_Path = Directory_List(Current_Sub_Directory)
    148.  
    149.                 'You can do something like store them in a listbox
    150.  
    151.                 'List1.AddItem Directory_Path
    152.                
    153.             Loop Until Current_Directory = (Get_Number_Of_Directories + Number_Of_Directories)
    154.            
    155.             Current_Directory = 0
    156.    
    157.         Next Current_Sub_Directory
    158.    
    159.     Next Current_Drive
    160.  
    161. End Sub

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Search for *.txt

    maybe it should be Total_Number_Of_Directories, just a typo
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: Search for *.txt

    Well, I've tried that but it just repeats a lot of times the same directory.

  6. #6

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: Search for *.txt

    I'll just start a new thread about this, 'cause i got new code.
    Thank you once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width