-
VB6 - FTP in VB
Hello everyone,
can someone please guide thru and FTP Process in VB environment.
We have an FTP tool call "WS_FTP PRO". Using this tool, we can mannually perform ftp functions(upload/import files to/from) mainframe or server.
In my case, I have a vb module that creates a text file "empl_msgs.txt".
My objective is to FTP the above file to a secure server.
I have created a table "tblFtpInfo" (this table contains my FTP authentication info such as: Open, 10.8.25.15,usrid, passwrd, put srcfile, destfile, quit).
What is the best/easiest programmatical syntax to accomplish the ftp process?
I have:
Code:
ftp.exe -s:"\\filepath and name\ftpinfo.txt"
Note: ftpinfo.txt contains my FTP authentication information, the open, put and quit statements.
Another important piece of this process is after vb turns control to the ftp execution, I want control to be turned back to vb so that vb can finish the rest of its execution.
Any assistance will be greatly appreciated.
Giftx.
-
Re: VB6 - FTP in VB
Thread moved from the FAQ forum, which is not the place to post your questions.
-
Re: VB6 - FTP in VB
you can use shell to execute the ftp command with the parameter file you have
-
Re: VB6 - FTP in VB
Like so:
Code:
Dim RetVal As String
RetVal = Shell("C:\Winnt\system32\FTP.EXE -s:MyFile.bat",1)
or
Code:
ftp.exe -s:"\\MyFilePath\MyFile.bat"
Which of the script are appropriate?
I understand that using the above, after the ftp assumes execution control, vb doesn't assume control when ftp finishes, causing it to hang.
Please advice - with possible examples if any.
Thanks.
Giftx.
-
Re: VB6 - FTP in VB
So you are saying that you have the above working but it pauses during the login/upload and you dont want that?
-
Re: VB6 - FTP in VB
vb should keep running, not wait for ftp to finish, and will get no results from ftp
i use ftp and internet apis to upload to server, then no need to use dos ftp
you could also use inet control or winsock control
if you want api code example search FTP posts by me
-
Re: VB6 - FTP in VB
I have some posts too with the FTP APIs. Also http://allapi.mentalis.org has a decent api ftp example too.
-
Re: VB6 - FTP in VB
Hi Rob, West.
thanks for your insight. However Rob, I don't see any ftp example post, even after I went to the link you provided.
Also, I saw some examples online, where some people used the however, I don't know where "Inet1" was declared/defined and I don't know what "Inet1" was declared/defined as.
For the above to work, do I need to import any references, components and how should I use it?
Giftx.
-
Re: VB6 - FTP in VB
-
Re: VB6 - FTP in VB
Rob,
please could you show me how because Inet is not among the controls I have on my vb - General.
how/where do I get it?
Giftx.
-
Re: VB6 - FTP in VB
Add it to your toolbox.
Right click toolbox > Components > select the Microsoft Inet control from the list > click OK.
-
Re: VB6 - FTP in VB
Also,
where should I put these codes/declaration? In my module or within my source code or Form_Load()
Code:
Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Const PassiveConnection As Boolean = True
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net
'E-Mail: [email protected]
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
'create a buffer to store the original directory
sOrgPath = String(MAX_PATH, 0)
'get the directory
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
'create a new directory 'testing'
FtpCreateDirectory hConnection, "testing"
'set the current directory to 'root/testing'
FtpSetCurrentDirectory hConnection, "testing"
'upload the file 'test.htm'
FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
'rename 'test.htm' to 'apiguide.htm'
FtpRenameFile hConnection, "test.htm", "apiguide.htm"
'enumerate the file list from the current directory ('root/testing')
EnumFiles hConnection
'retrieve the file from the FTP server
FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
'delete the file from the FTP server
FtpDeleteFile hConnection, "apiguide.htm"
'set the current directory back to the root
FtpSetCurrentDirectory hConnection, sOrgPath
'remove the direcrtory 'testing'
FtpRemoveDirectory hConnection, "testing"
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen
End Sub
Public Sub EnumFiles(hConnection As Long)
Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
'set the graphics mode to persistent
Me.AutoRedraw = True
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first file
hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'show the filename
Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
'show the filename
Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
Sub ShowError()
Dim lErr As Long, sErr As String, lenBuf As Long
'get the required buffer size
InternetGetLastResponseInfo lErr, sErr, lenBuf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLastResponseInfo lErr, sErr, lenBuf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub
thanks.
Giftx
-
Re: VB6 - FTP in VB
Well first you need to decide on which technique you are going to be using. The Inet control or APIs?
-
Re: VB6 - FTP in VB
If you'd rather use Shell(), then you can use the following wrapper that waits until the called application quits until VB code resumes.
Code:
Option Explicit
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Private Const STILL_ACTIVE As Long = 259
Private Declare Function OpenProcess Lib "kernel32.dll" ( _
ByVal lngDesiredAccess As Long, _
ByVal lngInheritHandle As Long, _
ByVal lngProcessID As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32.dll" ( _
ByVal lngProcess As Long, _
ByRef lngExitCode As Long) As Long
'-------------------------------------------------------------------------------
' ShellWait
'-------------------------------------------------------------------------------
' Description: Wraps Visual Basic's built-in Shell() function, resuming code
' execution only when the called application terminates.
'
' Parameters: - strPathFilename [in] The path (optional), filename and
' command-line (optional) of the application which Shell()
' should call.
' - eWindowState [in] The window style to be used by the called
' application.
'
' Return: The Process ID of the called application.
'-------------------------------------------------------------------------------
Public Function ShellWait( _
ByVal strPathFilename As String, _
ByVal eWindowState As VBA.VbAppWinStyle) As Long
Dim lngProcess As Long
Dim lngExitCode As Long
'Call the VB Shell function, storing the called application's Process ID.
ShellWait = Shell(strPathFilename, eWindowState)
'Open the process.
lngProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, ShellWait)
'Wait until the process terminates.
Do
GetExitCodeProcess lngProcess, lngExitCode
DoEvents
Loop While lngExitCode = STILL_ACTIVE
End Function
Example usage:
Code:
Private Sub Command1_Click()
ShellWait "c:\windows\notepad.exe", vbNormalFocus
MsgBox "Notepad has closed"
End Sub
-
Re: VB6 - FTP in VB
Hi Rob,
actually, I'd like to use the "Inet" controls, but I don't see this control among my vb controls. Where and how can I find and input/drag and drop it on my vb object?
Sorry for responding so late. I've been away from the office since last week.
Also, where would I put the API declarations within my vb code, if I decide to use the API's?
Thanks.
-
Re: VB6 - FTP in VB
Add the control to your toolbox.
Project > Components > select Microsoft Inet Control > OK. Drag n drop the control to your form from the toolbox. :)
the way that example code is setup, you would place it behind a form.
-
Re: VB6 - FTP in VB
Hi Rob,
I was able to add the "Microsoft Internet Control" component to my form.
However, the Inet object does not have the "Execute". It has "Inet.ExecWB".
When I tried to do:
Code:
InetFTP.Execute "ftp://mfuchk:[email protected]", "Put \\Livctrls03-08\Data1\APP\UNCLAIMW\UCCTest\Databases\DataFiles\Emp_Messages.txt W.APPHRMS.UCCHKMSG"
It gave me a "Runtime error 438 - Object does not support this property of method"
when I did:
Code:
InetFTP.ExecWB "ftp://mfuchk:[email protected]", "Put \\Livctrls03-08\Data1\APP\UNCLAIMW\UCCTest\Databases\DataFiles\Emp_Messages.txt W.APPHRMS.UCCHKMSG"
it gave me a Runtime Error 13 - Type Mismatch.
What am I doing wrong? Maybe wrong FTP syntax?
Thanks.
Giftx.
-
Re: VB6 - FTP in VB
Code:
'create a new directory 'testing'
FtpCreateDirectory hConnection, "testinghh"
'set the current directory to 'root/testing'
FtpSetCurrentDirectory hConnection, "testinghh"
'upload the file 'test.htm'
FtpPutFile hConnection, "C:\test.xml", "test.xml", FTP_TRANSFER_TYPE_UNKNOWN, 0
'rename 'test.htm' to 'apiguide.htm'
FtpRenameFile hConnection, "test.xml", "apiguide.htm"
'enumerate the file list from the current directory ('root/testing')
'EnumFiles hConnection
'retrieve the file from the FTP server
'FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
'delete the file from the FTP server
'FtpDeleteFile hConnection, "apiguide.htm"
'set the current directory back to the root
'FtpSetCurrentDirectory hConnection, sOrgPath
'remove the direcrtory 'testing'
'FtpRemoveDirectory hConnection, "testing"
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen
End Sub
Public Sub EnumFiles(hConnection As Long)
Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
'set the graphics mode to persistent
Me.AutoRedraw = True
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first file
hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'show the filename
Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
'show the filename
Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
Could anyone tell me how to write code to send an entire folder with all contents to the ftp please?
Thanks!
-
Re: VB6 - FTP in VB
This is a 7 year old thread. Please create a new thread and link back to this one for reference.
Thanks