Results 1 to 11 of 11

Thread: Do while...Dir error...

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 1999
    Posts
    51

    Post

    I have a loop that looks like the code below. Inside the sub createRequest there is another Dir command. The first time around the loop below everything works fine until it hits 'reqFile = Dir' it bombs out and says 'Run-time error 5: Invalid procedure call or argument.' If I comment out my Dir loop that's in the sub createRequest then I don't get the error. None of the variables are the same and everything is declared as public. Any ideas? Thanks.

    Code:
    Do While reqFile <> ""
        createRequest
        reqFile = Dir
    Loop

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    I would assume that you want to get all files in specified directory. Then you should change your code to something like this:

    Code:
    Dim strFile As String
    
    strFile = Dir("C:\MyDirectory\*.*")
    
    Do While strFile <> ""
        'Do your stuff here
        strFile = Dir
    Loop
    ------------------

    Serge

    Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819


  3. #3

    Thread Starter
    Member
    Join Date
    Jul 1999
    Posts
    51

    Post

    That's what I had. I forgot to put that line.
    Code:
    reqFile = Dir(reqFolder & "*.fxr")
    Do While reqFile <> ""
        createRequest
        reqFile = Dir
    Loop
    Here is the code that bombs out when it hits it. I'm trying to loop and make sequential file names for the files I create. I just want to check to see if the file exists and if it does the make sure I don't use the same file name.
    Code:
    Do While Dir(filDir & filName) <> ""
        tmpNum = Right("0000" & (tmpNum + 1), 4)
        filName = "FILE" & tmpNum & ".tst"
    Loop
    [This message has been edited by desquite (edited 02-01-2000).]

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Post the Code in the Timer Sub with the 2 Loops.

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  5. #5

    Thread Starter
    Member
    Join Date
    Jul 1999
    Posts
    51

    Post

    The timer sub calls createRequest sub which has the other loop that's below.

    Code:
    Private Sub Timer1_Timer()
        Timer1.Enabled = False
            
        lblStatus.Caption = "Searching for request..."
            
        reqFile = Dir(reqFolder & "*.fxr")
        Do While reqFile <> ""
            createRequest
            reqFile = Dir
        Loop
        
        lblStatus.Caption = "Sleeping..."
        sngTime = Timer
        Do While (Timer - sngTime) < 10
            DoEvents
        Loop
        
        Timer1.Enabled = True
    End Sub
    Code:
    'other code...
    Do While (Dir(fileDir & filName) <> "")
        tmpNum = Right("0000" & (tmpNum + 1), 4)
        filName = "FILE" & tmpNum & ".tst"
    Loop
    'other code...

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    The Problem is you're calling Dir() Twice and the 2nd Time it's Checking to see if a File Exists, this returns only 1 value, (the name of the File, if it exists), so when you go back to your initial Loop, the Value of Dir is no longer valid. I don't know how your CreateRequest Sub effects anything in the Timer, but you can store the Files in the 1st Loop into an Array, then Perform a For .. Next Loop using the Array Afterward, seperating the use of Dir Between Procedures.
    Code:
    Private Sub Timer1_Timer()
        Dim aFiles() As String
        Dim iFiles As Integer
    
        Timer1.Enabled = False
            
        lblStatus.Caption = "Searching for request..."
            
        reqFile = Dir(reqFolder & "*.fxr")
        While reqFile <> ""
            ReDim Preserve aFiles(iFiles)
            aFiles(iFiles) = reqFile
            iFiles = iFiles + 1
            reqFile = Dir
        Wend
        For iFiles = 0 To UBound(aFiles)
            CreateRequest    
        Next
    
        lblStatus.Caption = "Sleeping..."
        sngTime = Timer
        Do While (Timer - sngTime) < 10
            DoEvents
        Loop
        
        Timer1.Enabled = True
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  7. #7

    Thread Starter
    Member
    Join Date
    Jul 1999
    Posts
    51

    Post

    That worked Aaron. Thank you. Now what's the best way to clear the array aFiles()?

    The next time through the loop it dies b/c I delete the file that's in the array and it trys to find it again.

    Thanks.

  8. #8
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    The Array aFiles() should be cleared as soon as you exit the Timer, it's only Dimmed to be a Local Variable, check the value of iFiles is greater than Zero before allowing your CreateRequest Loop to Process, otherwise there'll be no entries in the Array.

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  9. #9

    Thread Starter
    Member
    Join Date
    Jul 1999
    Posts
    51

    Post

    I see what I did. I made those variables global to the program so I could use them in my other sub. I'll set it back the way you had it.

    Thanks a lot!

  10. #10
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Try this function, it works for me:
    Code:
    Private Function GetNewFile(ByVal sPath As String)
        Dim iIndex As Integer
        Dim sFile As String
        
        If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
        sFile = "FILE0000.tst"
        While Len(Dir(sPath & sFile)) And iIndex < 10000
            iIndex = iIndex + 1
            sFile = "FILE" & Right$("0000" & iIndex, 4) & ".tst"
        Wend
        If iIndex > 9999 Then sFile = ""
        GetNewFile = sFile
    End Function
    Usage: FileName = GetNewFile("C:\Folder\")

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  11. #11

    Thread Starter
    Member
    Join Date
    Jul 1999
    Posts
    51

    Post

    I'm still getting the same error. The function works fine Aaron but somewhere in there something's crapping out.

    I am calling the first while loop that contains a dir command from a timer. I set the timer to enabled at form_load and then disable it after I hit the timer1_timer sub. Then after the first while loop there is another while loop inside of it with a dir command. When I comment out the second dir command the program runs just fine.

    What in the world could it be crashing against to make this happen? I have the exact same code in another program I've done and it works fine.

    Thanks for all of the fast responses! I really do appreciate everyones help!

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