Sunday, July 4, 2010

FTPGetFile

Example of FTPGetFile

const service_ftp=1
const open_type_direct=1
const ftp_transfer_type_ascii=1

'Extern is a QuickTest object to declare an external functions
Extern.Declare miclong, "InternetOpen", "wininet.dll", "InternetOpenA",_
micString, micDWord, micString, micString, micDWord
Extern.Declare miclong, "InternetConnect", "wininet.dll", "InternetConnectA",_ micLong, micString, micInteger, micString, micString, micDWord, micDWord,_ micDWord
Extern.Declare micinteger, "FtpGetFile", "wininet.dll", "FtpGetFileA", micLong,_ micString, micString, micInteger, micDWord, micDWord, micDWord
Extern.Declare micinteger, "InternetCloseHandle", "wininet.dll",_ "InternetCloseHandle", miclong

hnd_internet=Extern.InternetOpen("QTP_FTP", open_type_direct, vbnullchar,vbnullchar,0)

If hnd_internet=0 then
msgbox "Connection is Failed - 1"
Else
msgbox "Connection is Passed - 1"
End if

hnd_connection=Extern.InternetConnect(hnd_internet, "ftp.microsoft.com", 21,"","",1,0,0)

If hnd_connection=0 then
msgbox "Connection is Failed - 2"
Else
msgbox "Connection is Successful - 2"
End If

ret_val = Extern.FtpGetFile(hnd_connection,"/MISC/INDEX.TXT","c:\local_index.txt",0,0,1,0)
msgbox ret_val

Extern.InternetCloseHandle(hnd_connection)
Extern.InternetCloseHandle(hnd_internet)


FtpGetFile retrieves a file from the FTP server and stores it under the specified file name, creating a new local file in the process. First parameter is the handle to the FTP session. Second parameter is the name of the file to be downloaded / retrieved. Third parameter is the name of the file to be created on the local machine (from the downloaded file). Fourth parameter indicates whether the function should proceed if a local file of the specified name already exists. If fourth parameter is TRUE and the local file exists, FtpGetFile fails. Fifth parameter are the file attributes of the new file. Sixth parameter is how file downloading will be handled in our case we have used - "ftp_transfer_type_ascii=1" and the last parameter is used if we are using callbacks. As we are not using callbacks so it is kept at 0.

Also See:
QTP FTP Main Post
FTP InternetOpen
FTP InternetConnect
FTPPutFile
FTPOpenFile
FTPSetCurrentDirectory
FTPCreateDirectory
FTPRenameFile
FTPRemoveDirectory
FTPDeleteFile