Visual Basic And FTP Project Sample

I recently received a few questions via various forums for Visual Basic on how to FTP files. The questions center specifically around scheduled jobs. So, I opted to write a quick little Visual Basic application utilizing the Inet control.

The biggest area of confusion with implementing the Inet control is understanding
that you've got to wait for the Inet control to return a response for each command
you send it.  Pay close attention to the WaitForResponse function in frmFTP
and you'll see what I'm referring to.  I've included the source code both inside
this page as well as the project files via the link below:
Visual Basic And FTP Sample Project

'  Here's the code for frmFTP
Option Explicit
Private msCurrentFile As String
Friend Sub FTPFile(ByVal sFTPServer As String, _
                   ByVal sFTPCommand As String, _
                   ByVal sFTPUser As String, _
                   ByVal sFTPPwd As String, _
                   ByVal sFTPSrcFileName As String, _
                   ByVal sFTPTgtFileName As String)
 Dim oFS As Scripting.FileSystemObject
 Dim sURL As String
 On Error GoTo FTPFileExit
 Me.HRG True
 msCurrentFile = ""
 Set oFS = New Scripting.FileSystemObject
    sURL = "ftp://" & sFTPServer
    Inet1.Protocol = icFTP
    Inet1.RequestTimeout = 60
    Inet1.RemotePort = 21
    Inet1.AccessType = icDirect
    Inet1.URL = sURL
    Inet1.UserName = sFTPUser
    Inet1.Password = sFTPPwd
   Select Case sFTPCommand
          Case "PUT"
                msCurrentFile = sFTPSrcFileName
                If oFS.FileExists(sFTPSrcFileName) = False 
Then GoTo FTPFileExit Inet1.Execute sURL, sFTPCommand & Space(1) &
sFTPSrcFileName & " " & sFTPTgtFileName Case "GET" msCurrentFile = sFTPTgtFileName If oFS.FileExists(sFTPTgtFileName) = True Then oFS.DeleteFile sFTPTgtFileName, True end if Inet1.Execute sURL, sFTPCommand & Space(1) &
sFTPSrcFileName & " " & sFTPTgtFileName End Select Me.WaitForResponse Inet1.Execute sURL, "quit" Me.WaitForResponse FTPFileExit: Set oFS = Nothing HRG False End Sub Friend Sub WaitForResponse() Dim fWait As Boolean On Error GoTo ErrHandler fWait = True Do Until fWait = False DoEvents fWait = Inet1.StillExecuting Loop ErrHandler: Err.Clear End Sub Private Sub Inet1_StateChanged(ByVal State As Integer) On Error Resume Next Select Case State Case icNone Case icResolvingHost:
Me.lblRESPONSE.Caption = "Resolving Host" Case icHostResolved:
Me.lblRESPONSE.Caption = "Host Resolved" Case icConnecting:
Me.lblRESPONSE.Caption = "Connecting..." Case icConnected:
Me.lblRESPONSE.Caption = "Connected" Case icResponseReceived:
Me.lblRESPONSE.Caption = "Transferring File..." Case icDisconnecting:
Me.lblRESPONSE.Caption = "Disconnecting..." Case icDisconnected:
Me.lblRESPONSE.Caption = "Disconnected" Case icError:
MsgBox "Error:" & Inet1.ResponseCode & " " & Inet1.ResponseInfo Case icResponseCompleted:
Me.lblRESPONSE.Caption = "Process Complete." End Select Me.lblRESPONSE.Refresh Err.Clear End Sub Friend Sub HRG(fShowHourGlass As Boolean) If fShowHourGlass = True Then Screen.MousePointer = 11 Else Screen.MousePointer = 0 End If End Sub Private Sub Form_Unload(Cancel As Integer) Set frmFTP = Nothing End Sub ' Here's the code for FTPMain.bas ' Remember to set your project start up to Sub Main() Public Sub Main() Load frmFTP frmFTP.Show frmFTP.FTPFile "my ip address", "PUT", "myuser",
"mypwd", "C:\temp\test.txt", "test.txt" frmFTP.FTPFile "my ip address", "GET", "myuser",
"mypwd", "test.txt", "c:\temp\test2.txt" Unload frmFTP End Sub
By Robbe Morris   Popularity  (16712 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Here's my most recent course on Pluralsight. I think it has some interesting insight on IT professional job interviews and using words in your resume to influence the questions you'll be asked. Resumes, Job Seeking, and Interviews in context.