CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2001
    Location
    USA
    Posts
    161

    FTP and Inet, how to make the program wait?

    Hi,
    I am trying to make a program that will download several files on several FTP servers every morning.
    And I am using a Inet component. Everything works fine when I work in step by step, but when I just launch the program, the program is not waiting that previous request is done before working on next, so it results that nothing is working

    Any idea on how I could fix this?
    Thanks!

    Here is the code:
    Code:
    Option Explicit
    Dim status As String
    Dim strData As String
    Private Const ForReading = 1, ForWriting = 2, ForAppending = 8
    
    Private Sub Form_Load()
        
        Dim FTPAddress As String, FTPLogin As String, FTPPass As String, l_LineData As String
        Dim l_Filename As String, l_DownloadedFilename As String, l_PathDownload As String
        Dim l_SeverChange As Boolean, l_KeepWorking As Boolean
        Dim fso As New FileSystemObject, fil As TextStream  ' As Files
        
        Set fil = fso.OpenTextFile(App.Path & "\GetFiles.txt", ForReading)
        If Err.Number <> 0 Then MsgBox "Opening GetFiles.txt failed. Reason: " & Err.Description
        
        FTPAddress = Mid(fil.ReadLine, 7)
        While Not fil.AtEndOfStream
            FTPLogin = Mid(fil.ReadLine, 7)
            FTPPass = Mid(fil.ReadLine, 7)
            'Login on the FTP
            Inet1.URL = FTPAddress
            Inet1.UserName = FTPLogin
            Inet1.Password = FTPPass
            status = "DIR"
            On Error Resume Next
            Inet1.Execute , "DIR"
            If Err.Number <> 0 Then 'Coun't connect? Pass to the next FTP SERVER
                l_KeepWorking = False
                ErrorList.AddItem "Connection to Server: " & FTPAddress & " FAILED, no file will be downloaded"
            Else
                l_KeepWorking = True
                OkList.AddItem "Connected to Server: " & FTPAddress
            End If
    
            l_SeverChange = False
            
            While Not fil.AtEndOfStream And Not l_SeverChange
      
                l_LineData = fil.ReadLine
                If Mid(l_LineData, 1, 6) = "ADDRS:" Then
                    l_SeverChange = True
                    FTPAddress = Mid(l_LineData, 7)
                Else
                    If l_KeepWorking Then
                        l_Filename = Mid(l_LineData, 7)
                        l_DownloadedFilename = Mid(fil.ReadLine, 7)
                        l_PathDownload = Mid(fil.ReadLine, 7)
                        DownloadFile l_Filename, l_DownloadedFilename, l_PathDownload
                    End If
                End If
            Wend
            On Error Resume Next
            If Inet1.StillExecuting Then
                timWorking.Enabled = True
                'try to cancel the request
                Inet1.Cancel 'It may take a while to cancel a request
            End If
            status = "CLOSE"
            Inet1.Execute , "CLOSE"
        Wend
    End Sub
    
    Private Function DownloadFile(p_Filename As String, p_NewFilename As String, p_path As String) As Boolean
        
        Dim PlaceStr1 As Integer, PlaceStr2 As Integer
        Dim DateToday As Date
        On Error Resume Next
        status = "GET"
        'Insert the date in the downloaded file
        PlaceStr1 = InStr(1, p_NewFilename, "*DATE")
        PlaceStr2 = InStr(PlaceStr1 + 1, p_NewFilename, "*")
        p_NewFilename = Mid(p_NewFilename, 1, PlaceStr1 - 1) & Format(CDate(Now), Mid(p_NewFilename, PlaceStr1 + 5, PlaceStr2 - PlaceStr1 - 5)) & Mid(p_NewFilename, PlaceStr2 + 1)
        Inet1.Execute , "GET " & p_Filename & " " & p_path & p_NewFilename
    
        If Err.Number <> 0 Then 'Coun't connect? Pass to the next FTP SERVER
           ErrorList.AddItem "Download File: " & p_Filename & " FAILED. Error:" & Err.Description
        Else
           OkList.AddItem "Downloaded File: " & p_Filename
        End If
    
    End Function
    Getfiles.txt looks like this :
    ADDRS:ftp://address/
    LOGIN:Login
    PASSW:password
    FILES:Downloadtext.txt
    NewNF:Downloadtext*DATEMMDDYYYY*.txt
    PATHF:C:/
    FILES:Downloadtext2.txt
    NewNF:Downloadtext2*DATEMMDDYYYY*.txt
    PATHF:C:/
    ---------------------------------------------
    Sure i got it... what s a form again?

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Exclamation Re: FTP and Inet, how to make the program wait?

    Quote Originally Posted by Kain
    Hi,
    I am trying to make a program that will download several files on several FTP servers every morning.
    And I am using a Inet component. Everything works fine when I work in step by step, but when I just launch the program, the program is not waiting that previous request is done before working on next, so it results that nothing is working

    Any idea on how I could fix this?
    Thanks!
    I'm not that good with INet, but here are a few thoughts on how to debug this problem.

    To me the code seem ok. but what I have noticed myself is that often while using the 'F8' key to step over the code, certain events do not trigger correctly... (I've noticed this in Winsock contl).

    Try placing a few 'debug.print "Code line ????" ' through out your code or writing out to a log file.. ( little more code but worth it).

    during execution you can trace where the program logic is running, It quite possible that the Form_Load function is been triggerd multiple times. but this you will find with a logged execution ..

    Hope this Helps

    Gremmy

  3. #3
    Join Date
    Apr 2001
    Location
    USA
    Posts
    161

    Re: FTP and Inet, how to make the program wait?

    Hi,
    Thanks for your help, but it seems that it goes to the right place at the right moment.. The problem is really that the program doesn't wait till one command is done before doing the next one. (The error message I get is : "Still executing last request")
    It's my first try on the inet component so I am obviously not a expert either.

    Thanks again from trying to help.
    ---------------------------------------------
    Sure i got it... what s a form again?

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Unhappy Re: FTP and Inet, how to make the program wait?

    Where abouts in the code are you getting this error ...

    Another thing you could try..

    Code:
    Private trigger as boolean
    trigger = false
    do until Trigger
        doevents
    loop
    and in the Inet1_StateChanged sub
    Code:
    Private Sub Inet1_StateChanged(ByVal State As Integer)
    if ('Process completed' ) then trigger = True
    End Sub
    Not to sure on how to check if the precess is complete.. Do some trial and error.

    Gremmy

  5. #5
    Join Date
    Apr 2001
    Location
    USA
    Posts
    161

    Re: FTP and Inet, how to make the program wait?

    Code:
    Private Function DownloadFile(p_Filename As String, p_NewFilename As String, p_path As String) As Boolean
        
       Inet1.Execute , "GET " & p_Filename & " " & p_path & p_NewFilename
    
        If Err.Number <> 0 Then 'Couldn't DL
           ErrorList.AddItem "Download File: " & p_Filename & " FAILED. Error:" & Err.Description
        Else
           OkList.AddItem "Downloaded File: " & p_Filename
        End If
    
    End Function
    This is where I get my error.

    And yes I did try what you are suggesting, and it doesn't work
    You can get if the proccess is complete like this :
    Code:
        If Not Inet1.StillExecuting Then 'Finished executing
    ---------------------------------------------
    Sure i got it... what s a form again?

  6. #6
    Join Date
    Dec 2004
    Posts
    423

    Re: FTP and Inet, how to make the program wait?

    Usually after all my FTP commands with INET I run this

    Code:
        Do
            DoEvents
        Loop While Inet1.StillExecuting
    And that runs fine for me.

  7. #7
    Join Date
    Apr 2001
    Location
    USA
    Posts
    161

    Re: FTP and Inet, how to make the program wait?

    I feel dum
    I tried so many things with the Inet1.StillExecuting.. But nothing that simple... and it works great..


    thanks a lot!
    ---------------------------------------------
    Sure i got it... what s a form again?

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: FTP and Inet, how to make the program wait?

    Quote Originally Posted by Sabin_33
    Usually after all my FTP commands with INET I run this

    Code:
        Do
            DoEvents
        Loop While Inet1.StillExecuting
    And that runs fine for me.
    Ahhh Much what i was aiming for But much smoother..

    Good One Sabin

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

Featured