Results 1 to 8 of 8

Thread: Searching File in Folders - Dir()

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Searching File in Folders - Dir()

    I am going through a directory and grabbing the folder names 4 deep and then grabbing the filename. I am having problems when it goes back up the go to the next folder I am getting an error message "Invalid procedure call or argument". I think it is because I have already called the Dir.

    Thanks
    Shannon

    VB Code:
    1. Private Function GetClient(iPath As String) As String
    2.  
    3.     Dim MyDir
    4.     Dim MyClient As String
    5.     MyClient = Dir(iPath, vbDirectory)
    6.    
    7.     Do While MyClient <> ""
    8.         If MyClient <> "." And MyClient <> ".." Then
    9.             If (GetAttr(iPath & MyClient) And vbDirectory) = vbDirectory Then
    10.                 Debug.Print MyClient
    11.                 Call GetCategory(iPath & MyClient & "\")
    12.                
    13.             End If
    14.         End If
    15.         MyClient = Dir
    16.     Loop
    17.        
    18.    
    19. End Function
    20.  
    21. Private Sub Form_Activate()
    22.    
    23.     Call GetClient("\\192.168.75.120\staging\")
    24.    
    25. End Sub
    26.  
    27. Private Function GetCategory(MyClient As String) As String
    28.    
    29.     Dim MyDir1
    30.     Dim MyCategory As String
    31.     MyCategory = Dir(MyClient, vbDirectory)
    32.    
    33.     Do While MyCategory <> ""
    34.         If MyCategory <> "." And MyCategory <> ".." Then
    35.             If (GetAttr(MyClient & MyCategory) And vbDirectory) = vbDirectory Then
    36.                 Debug.Print MyClient & MyCategory
    37.                 Call GetSource(MyClient & MyCategory & "\")
    38.             End If
    39.         End If
    40.         MyCategory = Dir
    41.     Loop
    42.    
    43. End Function
    44.  
    45. Private Function GetSource(MyCategory As String) As String
    46.    
    47.     Dim MyDir2
    48.     Dim MySource As String
    49.     MySource = Dir(MyCategory, vbDirectory)
    50.    
    51.     Do While MySource <> ""
    52.         If MySource <> "." And MySource <> ".." Then
    53.             If (GetAttr(MyCategory & MySource) And vbDirectory) = vbDirectory Then
    54.                 Debug.Print MyCategory & MySource
    55.                 Call GetGUID(MyCategory & MySource & "\HOLDING\")
    56.             End If
    57.         End If
    58.         MySource = Dir  <-- I am getting the error here after calling GetGUID
    59.     Loop
    60.    
    61. End Function
    62.  
    63. Private Function GetGUID(MySource As String) As String
    64.    
    65.     Dim MyDir3
    66.     Dim MyGUID As String
    67.     MyGUID = Dir(MySource, vbDirectory)
    68.    
    69.     Do While MyGUID <> ""
    70.         If MyGUID <> "." And MyGUID <> ".." Then
    71.             Debug.Print MyGUID
    72.         End If
    73.         MyGUID = Dir
    74.     Loop
    75.    
    76. End Function

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Searching File in Folders - Dir()

    im fairly sure you can only have 1 "Dir" going at a time.. each time its called its "reset"
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Searching File in Folders - Dir()

    A hack/workaround is to create a function which will reset Dir to the original location.

    VB Code:
    1. Private Function ResetDir(Path As String, Folder As String) As Boolean
    2.     Dim strFolder As String
    3.    
    4.     strFolder = Dir(Path, vbDirectory)
    5.     Do Until strFolder = Folder Or strFolder = ""
    6.         strFolder = Dir
    7.     Loop
    8.  
    9. End Function

    Call ResetDir after each call to one of your Get procedures.

    VB Code:
    1. Private Function GetClient(iPath As String) As String
    2.  
    3.     Dim MyDir
    4.     Dim MyClient As String
    5.     MyClient = Dir(iPath, vbDirectory)
    6.    
    7.     Do While MyClient <> ""
    8.         If MyClient <> "." And MyClient <> ".." Then
    9.             If (GetAttr(iPath & MyClient) And vbDirectory) = vbDirectory Then
    10.                 Debug.Print MyClient
    11.                 Call GetCategory(iPath & MyClient & "\")
    12.                 [B]ResetDir iPath, MyClient[/B]
    13.             End If
    14.         End If
    15.         MyClient = Dir
    16.     Loop
    17. End Function

  4. #4
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Searching File in Folders - Dir()

    I suggest using the file system object.

    Project->References include Microsoft Scripting Runtime.

    VB Code:
    1. Dim fsObj as New FileSystemObject
    2. Dim myFolder as Folder
    3.  
    4. For Each myFolder In fsObj.GetFolder(YourFolder).SubFolders
    5.    Debug.Print myFolder.Name
    6. Next

  5. #5
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Searching File in Folders - Dir()

    Here is the code to do what you need using filesystemobject and recursion

    VB Code:
    1. Private Sub Form_Activate()
    2.     PrintMyJunk 1, "c:\Program Files"
    3. End Sub
    4.  
    5. Public Sub PrintMyJunk(Level As Integer, MyDir As String)
    6.     Dim fsObj As New FileSystemObject
    7.     Dim myFolder As Folder
    8.  
    9.     For Each myFolder In fsObj.GetFolder(MyDir).SubFolders
    10.         Select Case Level
    11.             Case Is = 1
    12.                 Debug.Print "Client = " & myFolder.Name
    13.             Case Is = 2
    14.                 Debug.Print "Category = " & myFolder.Name
    15.             Case Is = 3
    16.                 Debug.Print "Source = " & myFolder.Name
    17.             Case Is = 4
    18.                 Debug.Print "GUID = " & myFolder.Name
    19.         End Select
    20.                
    21.         If Level < 4 Then
    22.             PrintMyJunk Level + 1, myFolder.Path
    23.         End If
    24.     Next
    25. End Sub
    Last edited by umilmi81; Oct 13th, 2005 at 05:57 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Re: Searching File in Folders - Dir()

    I have converted it over to the filesystemobject. My question is how do I get all of the filename in the folder?

    My code is below. Thanks everyone for your help.

    Shannon

    VB Code:
    1. Public Sub GetFileName(Level As Integer, MyDir As String)
    2.     Dim fsObj As New FileSystemObject
    3.     Dim myClient As Folder
    4.     Dim myCategory As Folder
    5.     Dim mySource As Folder
    6.     Dim myStatus As Folder
    7.     Dim myFile As File
    8.        
    9.     For Each myClient In fsObj.GetFolder(MyDir).SubFolders
    10.        
    11.         For Each myCategory In fsObj.GetFolder(myClient).SubFolders
    12.             For Each mySource In fsObj.GetFolder(myCategory).SubFolders
    13.                 For Each myStatus In fsObj.GetFolder(mySource).SubFolders
    14.                     If myStatus.Name = "Holding" Then
    15.                         'How do I get the filename
    16.                     End If
    17.                 Next
    18.             Next
    19.         Next
    20.     Next
    21.  
    22. End Sub

  7. #7
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Searching File in Folders - Dir()

    Well, i worte this some time ago (or did i ask for help on it :S can't remember) and it lists the filenames of a given folder quite easily.
    You can specify an extension if you want, OR, you could leave it blank retrieving the entire filename.

    VB Code:
    1. Private Sub ListFiles(strPath As String, Optional Extention As String)
    2.     Dim File As String
    3.         If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    4.         If Trim$(Extention) = "" Then
    5.         Extention = "*.*"
    6.     ElseIf Left$(Extention, 2) <> "*." Then
    7.         Extention = "*." & Extention
    8.     End If
    9.         File = Dir$(strPath & Extention)
    10.     Do While Len(File)
    11.         Debug.Print File
    12.         File = Dir$
    13.     Loop
    14. End Sub
    Zeegnahtuer?

  8. #8
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Searching File in Folders - Dir()

    Quote Originally Posted by odamsr
    I have converted it over to the filesystemobject. My question is how do I get all of the filename in the folder?

    My code is below. Thanks everyone for your help.

    Shannon

    VB Code:
    1. Public Sub GetFileName(Level As Integer, MyDir As String)
    2.     Dim fsObj As New FileSystemObject
    3.     Dim myClient As Folder
    4.     Dim myCategory As Folder
    5.     Dim mySource As Folder
    6.     Dim myStatus As Folder
    7.     Dim myFile As File
    8.        
    9.     For Each myClient In fsObj.GetFolder(MyDir).SubFolders
    10.        
    11.         For Each myCategory In fsObj.GetFolder(myClient).SubFolders
    12.             For Each mySource In fsObj.GetFolder(myCategory).SubFolders
    13.                 For Each myStatus In fsObj.GetFolder(mySource).SubFolders
    14.                     If myStatus.Name = "Holding" Then
    15.                         For Each myFile in myStatus.Files
    16.                             Debug.Print myFile.Name
    17.                         Next
    18.                     End If
    19.                 Next
    20.             Next
    21.         Next
    22.     Next
    23.  
    24. End Sub
    I modified your code to debug.print file names. Simple isnt' it?

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