FTP- Download, upload, Delete & find files

Public Function Func_FTP(Operation,ServerName,UserName,Password,RemoteLocation,LocalLocation)


'Set up the environment
Const INTERNET_OPEN_TYPE_DIRECT = 1

Extern.Declare micLong, "InternetOpen", "wininet.dll", "InternetOpenA", micString, micDWord, micString, micString, micDWord

Dim hInternet
hInternet = Extern.InternetOpen( "QTP_FTP", INTERNET_OPEN_TYPE_DIRECT, vbNullChar, vbNullChar, 0 )


If hInternet = 0 Then

Func_FTP= False

Reporter.ReportEvent micFail, "InternetOpen", "Failed to setup environment."

Exit Function

end If



' Open ftp server
Extern.Declare micLong, "InternetConnect", "wininet.dll", "InternetConnectA", micLong, micString, micInteger, micString, micString, micDWord, micDWord, micDWord

Const INTERNET_DEFAULT_FTP_PORT = 21

Const INTERNET_SERVICE_FTP = 1

Dim hConnect

hConnect = Extern.InternetConnect(hInternet,ServerName,INTERNET_DEFAULT_FTP_PORT,UserName,Password,INTERNET_SERVICE_FTP,0, 0 )

If hConnect = 0 Then

Func_FTP= False

Reporter.ReportEvent micFail, "InternetConnect", "Failed to open internet connection."

Exit Function

end If

Dim bRetval

Select case lcase(Operation)

Case "getfile"
'Get file
Extern.Declare micInteger, "FtpGetFile", "wininet.dll", "FtpGetFileA", micLong, micString, micString, micInteger, micDWord, micDWord, micDWord

Const FTP_TRANSFER_TYPE_ASCII = 1

bRetval = Extern.FtpGetFile( hConnect, RemoteLocation, LocalLocation, False, 0, FTP_TRANSFER_TYPE_ASCII, 0 )

wait(2)

If Not CBool( bRetVal ) Then

Func_FTP= False

Reporter.ReportEvent micFail, "FtpGetFile " & RemoteLocation, "Failed to download file. at " & LocalLocation

Exit Function

end If

Case "putfile"
'put file
Extern.Declare micInteger, "FtpPutFile", "wininet.dll", "FtpPutFileA", micLong, micString, micString, micDWord

bRetval = Extern.FtpPutFile( hConnect,LocalLocation,RemoteLocation, 0 )

wait(2)

If Not CBool( bRetVal ) Then

Reporter.ReportEvent micFail, "FtpPutFile " & LocalLocation , "Failed to upload file. at " & RemoteLocation

Func_FTP= False

Exit Function

End If


Case "deletefile"
'delete file
Extern.Declare micInteger, "FtpDeleteFile", "wininet.dll", "FtpDeleteFileA",micLong,micstring

bRetval =Extern.FtpDeleteFile(hConnect,RemoteLocation)

wait(2)

If Not CBool( bRetVal ) Then

Reporter.ReportEvent micFail, "FtpPutFile", "Failed to delete file.--" & RemoteLocation

Func_FTP= False

Exit Function

End If


Case "findfile"

Dim WIN32_FIND_DATA

Extern.Declare micInteger, "FtpFindFirstFile", "wininet.dll", "FtpFindFirstFileA", micLong, micString,miclong, micDWord

bRetval =Extern.FtpFindFirstFile(hConnect,RemoteLocation,WIN32_FIND_DATA,INTERNET_FLAG_NO_CACHE_WRITE)

bRetval=left(bRetval,2)

If bRetVal=0 Then

Func_FTP= False

Reporter.ReportEvent micFail, "File Not Found ", "File Not found ."

Exit Function

End If

Case else

End Select



'Close Connections
Extern.Declare micInteger, "InternetCloseHandle", "wininet.dll","InternetCloseHandle", micLong

Extern.InternetCloseHandle( hConnect )

Extern.InternetCloseHandle( hInternet )


End Function

Comments

  1. How to access the file attributes of the file found using findfile functions

    ReplyDelete
  2. What attributes you are looking for:

    You can find the file size by using FtpGetFileSize

    http://msdn.microsoft.com/en-us/library/aa384159(v=VS.85).aspx

    ReplyDelete
  3. Does the above library work for sFTP on port 22?

    I'm facing problems to upload a file... Can someone kindly mention the function call with parameters?

    ReplyDelete
  4. no wininet.dll don't support sFTP.You can refer following commercial libraries or google it for several other commercial libraries: http://winscp.net/eng/docs/commandline http://www.weonlydo.com/FtpDLX/ftp-sftp-ssl-component.asp

    ReplyDelete

Post a Comment

Popular posts from this blog

Arrays

What is the difference between eval, execute and executeglobal.

Sample Test Strategy for a MicroService Project with APIs only