Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inet Control Search for zip files in directory.

Status
Not open for further replies.

RainX

Programmer
Aug 20, 2007
4
Hi all,

I wanted to know how to search through a directory located on an ftp site (using inet control) and download all the zip files in that directory?

I am able to download a specific file, but im having trouble searching through it.

Thanks for any help

Take care,
--
 
You need to do a directory command. It returns a string you will have to parse. I posted a class that does all of that and more. You can find it here: thread222-1426823.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Hi ArtieChokie,

Thanks for your help
I tried your class and for some reason it keeps hanging on me.
I also tried a simple dir command and it gets all the file names but for some reason it hangs up.

Any more help or directions would be appreciated.

Thanks again
 
Where does it hang up?

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
When i try to close the application it just hangs.

Take care
 
Here's something i made, but i'm still having trouble with it. Maybe someone can point me in the right direction

Code:
Private Sub DLFiles()
     Dim i%
     Inet3.URL = mURL$
     Inet3.UserName = mUserid$
     Inet3.password = mPassword$
     ' First Command to change directories
     strCommand$ = "CD "
     Inet3.Execute "", strCommand$ + "/AB_" + mElec$ + mImportPath$
     Do While Inet3.StillExecuting
         DoEvents
     Loop
     
     ' second command to get a list of files to download
     
     strCommand$ = "DIR "
     Inet3.Execute "", strCommand$ + "*.TXT"
     Do While Inet3.StillExecuting
         DoEvents
     Loop
     
     If numFiles% > 0 Then
          For i% = 0 To UBound(sArray)
               impFiles$ = "c:\tempx\" & sArray(i%)
               strCommand$ = "GET "
               Inet3.Execute "", strCommand$ & " " & impFiles$
               Do While Inet3.StillExecuting
                    DoEvents
               Loop
          Next i%
     End If
     
     MsgBox "Done"
End Sub


My changestate event for the inet control is


Code:
Private Sub Inet3_StateChanged(ByVal State As Integer)
    Dim vtdata As Variant ' Data variable.
    Dim x$, i%
    Select Case State
   ' ... Other cases not shown.
   
       Case icError ' 11
        ' In case of error, return ResponseCode and  ResponseInfo.
        vtdata = Inet3.ResponseCode & ":" & Inet2.ResponseInfo
        List_status.AddItem vtdata

    Case icResponseCompleted ' 12
          
      ' Do if dir only
        If Trim$(strCommand$) Like "DIR" Then
              numFiles% = 0
              vtdata = Inet3.GetChunk(1024, icString)
              If LenB(vtdata) > 0 Then
                  Call cleanChunk(vtdata, numFiles%)
              End If
              If vtdata <> "" Then
                  sArray = Split(vtdata, Chr$(9))
                  List_status.AddItem "The following files need to be downloaded:"
                  For i = LBound(sArray) To UBound(sArray)
                      List_status.AddItem sArray(i)
                  Next
               Else
                    List_status.AddItem "No files need to be downloaded. Proceeding with import..."
               End If
'        ElseIf Trim$(strCommand$) Like "GET" Then
'               fh% = FreeFile
'               'Open mDirFile$ For Binary Access Write As #fh%
'               Open impFiles$ For Binary As #fh%
'               ' Get the first chunk. NOTE: specify a Byte
'               ' array (icByteArray) to retrieve a binary file.
'               vtdata = Inet2.GetChunk(1024, icString)
'               Do While LenB(vtdata) > 0
'                    Put #fh%, , vtdata
'                    ' Get next chunk.
'                    vtdata = Inet2.GetChunk(1024, icString)
'               Loop
'               Put #fh%, , vtdata
'               Close fh%
        End If
    End Select
    DoEvents
End Sub

And my cleanChunk function is

Code:
Private Sub cleanChunk(vtdata)
    Dim i%, k%, strData$, temp$
    
    For i = 1 To Len(vtdata) - 1
        k = InStr(i, vtdata, vbCrLf)
        strData = Mid(vtdata, i, k - i)
        i = k + 1
        If Trim$(strData$) <> "" Then
            If Trim$(temp$) <> "" Then
                temp$ = temp$ + Chr$(9) + strData$
            Else
                temp$ = strData$
            End If
        End If
        DoEvents
    Next i
    vtdata = temp$
End Sub


Am i doing something wrong? And also i got the part until "DIR" command to work, but other times it just hangs up and my Download command "GET" doesnt work at all! it doesnt even go to the changed state event.
Am i using it wrong?

Thanks once again
 
You can use the wildcard to get all files at once. Don't bother with the Inet control. Use command line.

GET -B *.ZIP

-David
2006 & 2007 Microsoft Most Valuable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
Are you downloading large files (megabytes)? I found it doesn't work very well if the files are large. It hung on me also.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Hi RainX,

Here is the code i have been using so far for testing purposes, add a form with a couple of text boxes for the url, user name and password, plus a text box and a list box.

Although i am only testing this on my local machine i have been able to transfer files upto 130 megs without any problems.

Option Explicit

Private m_GettingDir As Boolean

Private Sub AddMessage(ByVal msg As String)
txtResults.Text = txtResults.Text & vbCrLf & msg
txtResults.SelStart = Len(txtResults.Text)
End Sub

Private Sub cmdDownload_Click()
Dim host_name As String

Enabled = False
MousePointer = vbHourglass
txtResults.Text = "Working"
txtResults.SelStart = Len(txtResults.Text)
DoEvents

' You must set the URL before the user name and
' password. Otherwise the control cannot verify
' the user name and password and you get the error:
'
' Unable to connect to remote host
host_name = txtHost.Text
If LCase$(Left$(host_name, 6)) <> "ftp://" Then host_name = "ftp://" & host_name
inetFTP.URL = host_name

inetFTP.UserName = txtUserName.Text
inetFTP.Password = txtPassword.Text

' Do not include the host name here. That will make
' the control try to use its default user name and
' password and you'll get the error again.
inetFTP.Execute , "Get " & _
txtRemoteFile.Text & " " & txtLocalFile.Text


While inetFTP.StillExecuting
DoEvents
Wend

inetFTP.Execute , "Delete " & _
txtRemoteFile.Text

End Sub

Private Sub cmdUpload_Click()
Dim host_name As String

Enabled = False
MousePointer = vbHourglass
txtResults.Text = "Working"
txtResults.SelStart = Len(txtResults.Text)
DoEvents

' You must set the URL before the user name and
' password. Otherwise the control cannot verify
' the user name and password and you get the error:
'
' Unable to connect to remote host
host_name = txtHost.Text
If LCase$(Left$(host_name, 6)) <> "ftp://" Then host_name = "ftp://" & host_name
inetFTP.URL = host_name

inetFTP.UserName = txtUserName.Text
inetFTP.Password = txtPassword.Text

' Do not include the host name here. That will make
' the control try to use its default user name and
' password and you'll get the error again.
inetFTP.Execute , "Put " & _
txtLocalFile.Text & " " & txtRemoteFile.Text

End Sub

Private Sub Command1_Click()
Dim host_name As String

Enabled = False
MousePointer = vbHourglass
txtResults.Text = "Working"
txtResults.SelStart = Len(txtResults.Text)
DoEvents

' You must set the URL before the user name and
' password. Otherwise the control cannot verify
' the user name and password and you get the error:
'
' Unable to connect to remote host
host_name = txtHost.Text
If LCase$(Left$(host_name, 6)) <> "ftp://" Then host_name = "ftp://" & host_name
inetFTP.URL = "ftp://86.11.166.3" 'host_name

inetFTP.UserName = "stuart" 'txtUserName.Text
inetFTP.Password = "merlin1234" 'txtPassword.Text
m_GettingDir = True
inetFTP.Execute , "Dir " & "*.txt"

While inetFTP.StillExecuting
DoEvents
Wend
m_GettingDir = False
MsgBox (Str(list_status.ListCount))

End Sub

Private Sub inetFTP_StateChanged(ByVal State As Integer)
Dim sArray() As String
Dim tempString As String
Dim i As Integer
Select Case State
Case icError
AddMessage "Error: " & _
" " & inetFTP.ResponseCode & vbCrLf & _
" " & inetFTP.ResponseInfo
Case icNone
AddMessage "None"
Case icConnecting
AddMessage "Connecting"
Case icConnected
AddMessage "Connected"
Case icDisconnecting
AddMessage "Disconnecting"
Case icDisconnected
AddMessage "Disconnected"
Case icRequestSent
AddMessage "Request Sent"
Case icRequesting
AddMessage "Requesting"
Case icReceivingResponse
AddMessage "Receiving Response"
Case icRequestSent
AddMessage "Request Sent"
Case icResponseReceived
AddMessage "Response Received"
Case icResolvingHost
AddMessage "Resolving Host"
Case icHostResolved
AddMessage "Host Resolved"

Case icResponseCompleted
AddMessage inetFTP.ResponseInfo

If m_GettingDir Then
Dim txt As String
Dim chunk As Variant

m_GettingDir = False

' Get the first chunk.
chunk = inetFTP.GetChunk(1024, icString)
DoEvents
Do While Len(chunk) > 0
txt = txt & chunk
chunk = inetFTP.GetChunk(1024, icString)
DoEvents
Loop
'each filename will be delimited with 2 return chars, so get rid
'of first one and split out filenames
sArray = Split(txt, Chr$(13))
'now get rid of remaining line feed
For i = LBound(sArray) + 1 To UBound(sArray)
sArray(i) = Right(sArray(i), Len(sArray(i)) - 1)
Next

For i = LBound(sArray) To UBound(sArray)
If (sArray(i) <> "") Then
list_status.AddItem sArray(i)
End If
Next


AddMessage "----------"
AddMessage txt
End If

Case Else
AddMessage "State = " & Format$(State)
End Select

Enabled = True
MousePointer = vbDefault
End Sub

Hope this helps

Stuart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top