Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2003
    Location
    Brampton, Ontario, Canada
    Posts
    27

    Unanswered: Help creating Do Loop Code

    Hi,
    I need some help looping thru the following code.
    I have several directories that I want to find files in and perform some action on the found files. However the files reside in 8 different directories. I have the following code that finds the files in a specific directory.
    Set fs = Application.FileSearch
    With fs
    .LookIn = "F:\Documents\Dir1"
    .FileName = "*.txt"
    If .Execute > 0 Then
    For i = 1 To .FoundFiles.Count
    'My code to process on the found file name
    Next i
    Else
    MsgBox "There were no files found." _
    Exit Function
    End If
    End With


    Currently I have this code written 8 times.
    Once for each directory.

    Is there any way to create a variable for the .Lookin Directory
    and reset the variable to dir2, dir3, etc... for each loop
    after it finds or doesn't find files.

    Thanks for any help
    Brad

  2. #2
    Join Date
    Nov 2003
    Posts
    9
    Hullo. Try this:
    Code:
    Dim x as Integer
    x = 1
    Do Until x = 9
      Set fs = Application.FileSearch
      With fs
        .LookIn = "F:\Documents\Dir1"
        .FileName = "*.txt"
        If .Execute > 0 Then
          For i = 1 To .FoundFiles.Count
         'My code to process on the found file name
         Next i
         Else
         MsgBox "There were no files found." _
         Exit Function
       End If
      End With
    Loop
    NOTE: The above is UNTESTED, but should get you rolling.

    HTH

    P

  3. #3
    Join Date
    Jul 2003
    Location
    Earth | North America | United States | California | Long Beach
    Posts
    62
    Depending on the file structure that you would like to search, you could
    perhaps do this...

    .LookIn = "F:\Documents\"
    .SearchSubFolders = True

    Jack

  4. #4
    Join Date
    Mar 2003
    Location
    Brampton, Ontario, Canada
    Posts
    27
    Thanks Guys,
    This is a start. but I'd really like to figure out the logic for looping
    because when i can set a variable for the .Lookin directory for each loop
    I'll need to also set another variable within my code for the same loop.
    Any other ideas? Cause my brain is sore. I just have great difficulty working the logic thru.

  5. #5
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238

    Re: Help creating Do Loop Code

    Originally posted by BradCO
    Hi,
    I need some help looping thru the following code.
    I have several directories that I want to find files in and perform some action on the found files. However the files reside in 8 different directories. I have the following code that finds the files in a specific directory.
    Set fs = Application.FileSearch
    With fs
    .LookIn = "F:\Documents\Dir1"
    .FileName = "*.txt"
    If .Execute > 0 Then
    For i = 1 To .FoundFiles.Count
    'My code to process on the found file name
    Next i
    Else
    MsgBox "There were no files found." _
    Exit Function
    End If
    End With


    Currently I have this code written 8 times.
    Once for each directory.

    Is there any way to create a variable for the .Lookin Directory
    and reset the variable to dir2, dir3, etc... for each loop
    after it finds or doesn't find files.

    Thanks for any help
    Brad
    You could put your code into a function and call the function for each directory you want to search in. eg

    Private Function FindFiles(strDirectory As String) As Boolean
    Dim fle As Scripting.File
    Dim fsoSysObj As Scripting.FileSystemObject
    On Error Goto ErrorHere
    Set fsoSysObj = New Scripting.FileSystemObject
    For Each fle In fsoSysObj.GetFolder(strDirectory).Files
    ' we accept any file except a link file
    If fle.Type = "text" Then
    ' Do something with the file
    End If
    Next fle
    FindFiles=True
    ExitHere:
    Set fle= Nothing
    Set fsoSysObj = Nothing
    Exit Function
    ErrorHere:
    MsgBox "Error In: Form '" & Me.Name & "'" & vbCrLf _
    & "Procedure: FindFiles" & _
    vbCrLf & "Error Code: " & Err.Number & _
    vbCrLf & "Error: " & Err.Description, vbExclamation, "Error Alert"
    FindFiles= False
    Resume ExitHere
    End Function

    I have used the Scripting tool rather then the filesearch object as it is more powerful and you an do a lot with it. If you don't want to use it you could apply it to the FileSearch object using .LookIn = strDirectory. Just loop through the folders calling the function each time.

  6. #6
    Join Date
    Mar 2003
    Location
    Brampton, Ontario, Canada
    Posts
    27
    Thanks for the help,
    But this is becoming far more complicated than I expected.
    I'm a complete novice when it comes to logic in the code.
    Especially loops and if - then, do until, do while...
    It's all a little overwhelming for me.
    I guess i'll have to stay wiht my duplicate code for each Folder using the same code 8 times instead of looping.

Posting Permissions

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