Discussion Locked

VB and FTP

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I am looking to write a VB program to open an FTP program, select a couple of files and send them out on a daily, weekly or monthly basis. I have most of it written, except for the opening and sending of the FTP program. Any ideas on some code, orat least a good place to start would be very helpful.

8 replies

by tbradley

I'm not sure how VB handles dos command line prompts, but windows has ftp built in

open up the command prompt, and use the CD command to get to the directory where the file resides

c:\ftp xxx.xxx.xxx.xxx or c:\ftp filesource.com |echo y=username |echo y=password

you can then use the get and send commands to fetch and upload files.

In fact, you don't even have to use VB, write a dos batch file that does this, and use windows scheduler to launch it.

by tclere

This was taken from Tek-Tips.com!
This uses the internet transfer control that comes with VB. This code has the major components. Each time you ask it to do something - wait for it to finish.

Public Sub WaitForIC(ic As Inet)
'waits for an internet transfer control (ic)
'to finish what it's doing

With ic
Do
DoEvents
Loop While .StillExecuting
End With

End Sub

Public Function XferFile(itc as Inet,fname As String) As Boolean

On Error GoTo XferFile_ErrHandler

'Transfers a file to a target machine.

Dim lsHostName As String
Dim lsUserName As String
Dim lsPassword As String
Dim lsTmpDirName As String
Dim lsTargetdir As String

Dim lsCDcmd As String
Dim lsPUTcmd As String
Dim lsMsg As String

'XferFile = True: Exit Function

If (fname = vbNullString) Then
XferFile = False
Exit Function
End If

Call WaitForIC(itc)

lsHostName = "some.computer.com"
lsUserName = "valid_user_name"
lsPassword = "valid_password"
lsTargetdir = "dir_on_lsHostName"'lsPUTcmd = "PUT " & lsTmpDirName _
& fname & " " & lsTargetdir & fname _
& " PUT " & lsTmpDirName & fname & " " _
& lsTargetdir & "tmpthing.txt"

itc.AccessType = icDirect
Call WaitForIC(itc)

itc.Protocol = icFTP
Call WaitForIC(itc)

itc.URL = "ftp://" & lsHostName
Call WaitForIC(itc)

itc.UserName = lsUserName
Call WaitForIC(itc)

itc.Password = lsPassword
Call WaitForIC(itc)

itc.Execute itc.URL, lsPUTcmd
Call WaitForIC(itc)

XferFile = True

Exit Function

XferFile_ErrHandler:

lsMsg = "An Error has occurred while transferring a file to the Target Computer."

If mLastInetResponseCode <> 0 Then
lsMsg = lsMsg & " The last recorded response from the Target Computer was:" & vbCrLf & vbCrLf & mLastInetResponseCode & ": " & mLastInetResponseInfo & vbCrLf
End If

txtConvError.Text = lsMsg & vbCrLf & vbCrLf & txtConvError.Text

XferFile = False
Exit Function

End Function

by gbworld

Okay, here are a couple of things that you should not do:

1. Do not shell the command line FTP program. While this can be done, it is more trouble than it is worth and more likely to cause problems than solve them (answer 1).

Answer 2 is much better if you wish to use the Winsock control in Visual Basic. The question here is how you want to control the update (pure FTP, combination of HTTP and FTP). Here are some options:

1. Use the wininet API to use FTP. I like this as it is the mostflexible. It is a bit harder to program. There are a number of FTP samples on vbcode.com using WinInet (do a search for "FTP" at www.vbcode.com). Most of your programming will focus around these API calls:

Public Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" (ByVal hFtpSession As Long, ByVal sFileName As String, ByVal lAccess As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean

You can add these functions from the API viewer. Note that you will use both MSDN and the sample code to figure this one out.

2. If you plan on HTTP with FTP capability, you can use any form of upload component (like SAFileUP) on your server, along with the WinInet control on the client. This may be a bit more difficult, but can abstract the location of the files better than an FTP server can.

I hope this helps:

Gregory A. Beamer
MCP: +I, SE, SD, DBA

This discussion is closed to new replies.