• Search in Directory Twice

    Author
    Topic
    #462759

    I am having a challenge where I need to include two searches for file names in my VBA code.

    The code does like like the 2nd reference to the DIR statement and errors out.

    [codebox]Example:
    strFileName = Dir(MyFirstPath & MyFile)

    While strFileName “”

    ‘Change File Attributes/Delete existing File
    Results = Dir(MyPath & MyFile)
    If Results = “” Then
    ‘Do Nothing
    Else
    SetAttr “” & MyPath & MyFile & “”,vbNormal
    Kill MyPath & MyFile
    End If

    ‘Now copy newer file to MyPath
    Run some code to copy newer file to MyPath

    WEnd
    [/codebox]

    Your suggestions are appreciated,
    John

    Viewing 0 reply threads
    Author
    Replies
    • #1178807

      You cannot use Dir with a different path within a Dir loop – it messes up the Dir loop. You could store the filenames from the Dir loop in an array, then loop through the items of the array. You can safely use Dir within the second loop.

      BTW you need a line

      strFileName = Dir

      just above WEnd.

    Viewing 0 reply threads
    Reply To: Search in Directory Twice

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: