Jump to content

Example of _FTP_GetLastResponseInfo() and _FTP_Command()


argumentum
 Share

Recommended Posts

I spent a lot of time looking for an example of _FTP_Command() and of _FTP_GetLastResponseInfo().
When I finally figure it out. It was simple. So, here is an example for the next desperate soul trying to make sense of it.
Also a FtpFileAttribute2Str() .

#include <FTPEx.au3>
#include <MsgBoxConstants.au3>
#include <SQLite.au3>
#include <Debug.au3>

; this is to show how _FTP_Command() and  _FTP_GetLastResponseInfo() can be used.
; ..as a bonus, you also get FtpFileAttribute2Str()

_DebugSetup(StringTrimRight(@ScriptName, StringLen(".exe")) & ' example', True)
GUICtrlSetFont(-1, 10, 400, 0, "Courier New") ; $__g_hReportEdit_Debug font change :)

Local $sServer = 'ftp.uconn.edu' ; ..found at "https://dlptest.com/ftp-test/"
Local $sUsername = ''
Local $sPass = ''
Local $iError, $sMessage

Local $hOpen = _FTP_Open('MyFTP Control')
If Not @error Then
    Local $pCallback = _FTP_SetStatusCallback($hOpen, 'FTPStatusCallbackHandler')
    _FTP_GetLastResponseInfo($iError, $sMessage)
    _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
    Local $hConn = _FTP_Connect($hOpen, $sServer, $sUsername, $sPass, 0, $INTERNET_DEFAULT_FTP_PORT, $INTERNET_SERVICE_FTP, 0, $pCallback)
    If Not @error Then
        _FTP_GetLastResponseInfo($iError, $sMessage)
        _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
        Local $aFile = _FTP_ListToArrayEx($hConn, 0)
        If Not @error Then
            _FTP_GetLastResponseInfo($iError, $sMessage)
            _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
            $aFile[0][1] = "Filesize"
            $aFile[0][2] = "FileAttribute"
            $aFile[0][3] = "File Modification datetime"
            $aFile[0][4] = "File Creation datetime"
            $aFile[0][5] = "File Access datetime"
            For $n = 1 To UBound($aFile) - 1
                $aFile[$n][2] = $aFile[$n][2] & " [" & FtpFileAttribute2Str($aFile[$n][2]) & "]"
            Next
            _DebugOut(">=============" & @CRLF & _SQLite_Display2DResult($aFile, 0, True) & @CRLF & "=============<" & @CRLF)
        Else
            MsgBox($MB_SYSTEMMODAL, "Error", '_FTP_ListToArrayEx($Conn, 0)' & @CRLF & _
                    '@error = ' & @error & ' @extended = ' & @extended)
        EndIf
        Local $hcmd = _FTP_Command($hConn, 'STAT' & @CRLF, $FTP_TRANSFER_TYPE_ASCII, 0) ; or SYST, HELP, etc.
        _FTP_GetLastResponseInfo($iError, $sMessage)
        _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
        Sleep(100)

        Local $iFtpc = _FTP_Close($hConn)
        _FTP_GetLastResponseInfo($iError, $sMessage)
        _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
    Else
        _FTP_GetLastResponseInfo($iError, $sMessage)
        _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
        MsgBox($MB_SYSTEMMODAL, "Error", '_FTP_Connect($Open, ' & $sServer & ', ' & $sUsername & ', ' & $sPass & ')' & @CRLF & _
                '@error = ' & @error & ' @extended = ' & @extended)
    EndIf
    Local $iFtpo = _FTP_Close($hOpen)
    _FTP_GetLastResponseInfo($iError, $sMessage)
    _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
Else
    _FTP_GetLastResponseInfo($iError, $sMessage)
    _DebugOut(">=============" & @CRLF & $sMessage & @CRLF & "=============<" & @CRLF)
    MsgBox($MB_SYSTEMMODAL, "Error", "_FTP_Open('MyFTP Control')" & @CRLF & _
            '@error = ' & @error & ' @extended = ' & @extended)
EndIf

Func FTPStatusCallbackHandler($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext
    If $iInternetStatus = $INTERNET_STATUS_REQUEST_SENT Or $iInternetStatus = $INTERNET_STATUS_RESPONSE_RECEIVED Then
        Local $iBytesRead
        Local $tStatus = DllStructCreate('dword')
        _WinAPI_ReadProcessMemory(_WinAPI_GetCurrentProcess(), $pStatusInformation, $tStatus, $iStatusInformationLength, $iBytesRead)
        _DebugOut(_FTP_DecodeInternetStatus($iInternetStatus) & ' | Size = ' & DllStructGetData($tStatus, 1) & ' Bytes    Bytes read = ' & $iBytesRead)
    Else
        _DebugOut(_FTP_DecodeInternetStatus($iInternetStatus))
    EndIf
EndFunc   ;==>FTPStatusCallbackHandler

Func FtpFileAttribute2Str($i)
    Local Static $a[12][2] = [[11, ""], [0x01, "R"], [0x02, "H"], [0x04, "S"], [0x10, "D"], [0x20, "A"], _
            [0x80, "N"], [0x0100, "T"], [0x0800, "C"], [0x1000, "O"], [0x2000, "I"], [0x4000, "E"]]
;~      0x01 = "R" ; FILE_ATTRIBUTE_READONLY
;~      0x02 = "H" ; FILE_ATTRIBUTE_HIDDEN
;~      0x04 = "S" ; FILE_ATTRIBUTE_SYSTEM
;~      0x10 = "D" ; FILE_ATTRIBUTE_DIRECTORY
;~      0x20 = "A" ; FILE_ATTRIBUTE_ARCHIVE
;~      0x80 = "N" ; FILE_ATTRIBUTE_NORMAL
;~      0x0100 = "T" ; FILE_ATTRIBUTE_TEMPORARY
;~      0x0800 = "C" ; FILE_ATTRIBUTE_COMPRESSED
;~      0x1000 = "O" ; FILE_ATTRIBUTE_OFFLINE
;~      0x2000 = "I" ; FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;~      0x4000 = "E" ; $FILE_ATTRIBUTE_ENCRYPTED
    Local $n, $s = ""
    For $n = 1 To 11
        If BitAND($i, $a[$n][0]) = $a[$n][0] Then
            $s &= $a[$n][1]
        Else
            $s &= "."
        EndIf
    Next
    Return $s
EndFunc   ;==>FtpFileAttribute2Str

Hope this helps.

Edited by argumentum
fixed the code a bit

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...