Jump to content

Pulling a directory listing from a public ftp


Carm01
 Share

Recommended Posts

Hello,

I am attempting to pull a list of the directory structure from a public FTP where no username or password is required i.e:

ftp://ftp.adobe.com/pub/adobe/

Now I have looked all over the place and have failed find anything to accomplish, and if I found some, and the documentation is rather bleak for example;

it does show something I am looking for, but there is no ftp.au3, and the usage and examples of what i want to do seems to elude me on this and it may not even apply to what I am trying to accomplish?

I want to avoid using things with Internet explorer , and I have done some google searches. However nothing seems to help

the documentation surrounding : _FTP_DirGetCurrent  references _FTP_Connect , and then references _FTP_Open , and regardless what I try I cannot get it to pull a list of directorys of files as a list.

 

Any help is appreciated

 

 

 

Link to comment
Share on other sites

The example provided as it is written in the official documentation does not work, it errors out!

NbFound = 0  -> Error code: 1
"C:\Users\username\Desktop\testFTP.au3" (12) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
ConsoleWrite('$sFileName = ' & $aFile[1] & @CRLF)
ConsoleWrite('$sFileName = ' & ^ ERROR

How am i suppose to understand an example in the documentation fails?

I replaced :

Local $aFile = _FTP_ListToArray($hConn, 2)
ConsoleWrite('$NbFound = ' & $aFile[0] & '  -> Error code: ' & @error & @CRLF)
ConsoleWrite('$sFileName = ' & $aFile[1] & @CRLF)

with

_ArrayDisplay($aFile)

results in nothing

replaced the site with ftp://ftp.adobe.com/pub/adobe/

or ftp.adobe.com/pub/adobe/

Played around with the option, utter failure

 

the example given does not even work, so

Link to comment
Share on other sites

@Carm01 The example fails because the FTP server used in it no longer exist. Also you are attempting to use the URI of the directory as the hostname for the server, it won't work. Try this:

#include <Array.au3>
#include <FTPEx.au3>

Local $sServer = 'ftp.adobe.com'
Local $sUsername = ''
Local $sPass = ''

Local $hOpen = _FTP_Open('MyFTP Control')
Local $hConn = _FTP_Connect($hOpen, $sServer, $sUsername, $sPass)

_FTP_DirSetCurrent($hConn, "pub/adobe") ; Set the directory

Local $aFolders = _FTP_ListToArray($hConn, 1)

_ArrayDisplay($aFolders)

Local $iFtpc = _FTP_Close($hConn)
Local $iFtpo = _FTP_Close($hOpen)

Note the usage of _FTP_DirSetCurrent to change the current working directory on the FTP server

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Try this script:

#include <FTPEx.au3>
#include <File.au3>
#include <GuiEdit.au3>
Opt('MustDeclareVars', 1)
Global $bCancel=False ; wird benutzt um den Download abzubrechen
Global $bReturn=False ; wird benutzt um die rekursive Suche abzubrechen
Global  $sLogfile, $hFileLog
Global  $sDestFile = 'FilesOnFTP.txt'
    $sLogfile = StringReplace($sDestFile, '.txt', '.log')
    $hFileLog = FileOpen($sLogfile, 18)
    Global $iDepth = -1
    Global $aFiles[1][6], $hLog, $iDebug = 0
    Global $l_FTPSession, $l_InternetSession, $errOpen, $errFTP
    Global $sServer = 'ftp.csx.cam.ac.uk', $sUser = '', $sPass = '', $iPassive=1
    $l_InternetSession = _FTP_Open('AutoItZilla') ;Öffnet eine FTP Sitzung
    $errOpen = @error
    If Not @error Then
        _Report('Internetsitzung geöffnet ' , $hLog, 1)
        $l_FTPSession = _FTP_Connect($l_InternetSession, $sServer, $sUser, $sPass, $iPassive) ;Verbindet zu einem FTP Server
        $errFTP = @error
        If Not @error Then
            _Report('verbunden mit ' & $sServer, $hLog, 1)
            HotKeySet("{Esc}", "captureEsc") ;definiert ESCape-Taste für Abbruch der DL's
            _FTP_RecListToArrayEx($l_FTPSession, $aFiles, '/', False, $iDepth, $iDebug)
            ;_ArraySort($aFiles, 1, 1, 0, 1)
            _FileWriteFromArray($sDestFile, $aFiles)
            ShellExecute($sDestFile)
            _FTP_Close($l_FTPSession)
            _Report('Ftpsitzung geschlossen', $hLog, 1)
            _FTP_Close($l_InternetSession)
            _Report('Internetsitzung geschlossen', $hLog, 1)
            $l_InternetSession = -99
            $l_FTPSession = -99
        Else
            _Report('Verbindung mit ' & $sServer & ' fehlgeschlagen: ' & $errFTP, $hLog, 1)
        EndIf
    Else
        _Report('öffnen der Internetsitzung fehlgeschlagen: ' & $errOpen, $hLog, 1)
    EndIf
    _Report('======================= End of Loop =======================')
    _SumSize($aFiles)
    _FileWriteLog('FTPLoop.txt', $aFiles[0][0]&'|'&$aFiles[0][1] & @CRLF)

    FileClose($hFileLog)


Func captureEsc()
    $bCancel = True ;$bCancel ist in FTP_FileList_Recursiv.au3 Global definiert,
    ;wenn True wird der DL nach Userbestätigung abgebrochen
EndFunc   ;==>captureEsc

; Function Name:    _FTP_RecListToArrayEx($l_FTPSession, $aResult, $sPath, $bFolders=False,$iDebug=0,$b2D=False, $hLog=0,$hParent=0)
; Description::     füllt ein Array mit Dateien (und Verzeichnissen wenn $bFolders true) des FTP-Pfades inkl. aller Unterverzeichnisse
; Parameter(s):     $l_FTPSession   das von _FTP_Connect zurückgegebene Handle
;                   $aResult   = das Array in dem die Ergebnisse eingefügt werden
;                   $sPath     = Verzeichnispfad, der eingefügt werden soll
;                   $bFolders    = True = auch Verzeichnisse einfügen, False = nur Dateien, Default = nur Dateien
;                   $iMaxDepth  = <1 = Rekursion bis zum letzten Ästchen, jede andere Zahl stellt die max. Rekursionstiefe ein
;                   $iDebug sollen Debuginfos in der Scitekosole angezeigt werden? Default = 0 = keine, 1=mit, 2)mit + Arraydisplay
;                   $hLog       Handle/ControlID des EditControls für LOG-Ausgaben                                 optionale Angabe
;                   $hParent    das Handle der Gui                                  optionale Angabe
;                   $iDepth für internen Gebrauch daher nicht selbst setzen
; Autor(s):         AutoBert (www.autoit.de)
;                   zur freien Benutzung in eigenen Skripten/EXE'n
;                   und auch zur freien Weitergabe unter der Vorgabe,
;                   daß diese Funktionsbeschreibung inkl. Autor nicht enfernt wird.
;                   Bei EXE'n wäre es nett mich zu erwähnen, ist aber keine Pflicht.
; modified:         durch/Grund hier anfügen
;===============================================================================
Func _FTP_RecListToArrayEx($l_FTPSession, ByRef $aResult, $sPath, $bFolders = False, $iMaxDepth = -1, $iDebug = 0, $b2D = False, $hLog = 0, $hParent = 0, $iDepth = 0)
    Local $aFiles, $aFolders
    If $hFileLog <= 0 Then $hFileLog = FileOpen('Logfile.txt', 18)
    If $sPath = "" Then $sPath = "/"
    $sPath = StringReplace($sPath, '//', '/')
    _report($sPath & @TAB & $iDepth & '/' & $iMaxDepth, 0, $iDebug)
    If $bCancel And Not $bReturn Then _RealyCancel($hLog, $hParent)
    If $bCancel Then $bReturn = True
    If $bReturn Then Return
    If $iMaxDepth >= 0 Then
        If $iDepth > $iMaxDepth Then Return
    EndIf
    If UBound($aResult) = 1 Then
        $aResult[0][1] = 'Size'
        $aResult[0][2] = 'Atribute'
        $aResult[0][3] = 'Modification'
        $aResult[0][4] = 'Creation'
        $aResult[0][5] = 'Last Access'
    EndIf
    $iDepth += 1
    _FTP_DirSetCurrent($l_FTPSession, $sPath)
    If @error Then
        _report("Remoteverzeichnis: " & $sPath & " existiert nicht!", 0, $iDebug)
        Return
    Else
        $sPath = _FTP_DirGetCurrent($l_FTPSession) & '/'
    EndIf
    $sPath = StringReplace($sPath, '//', '/')
    $aFiles = _FTP_ListToArrayEx($l_FTPSession, 2)
    If IsArray($aFiles) Then
        If $iDebug = 2 Then _ArrayDisplay($aFiles, 'Dateien: ' & $sPath)
        If $aFiles[0][0] > 0 Then
            For $i = 1 To $aFiles[0][0]
                $aFiles[$i][0] = $sPath &  $aFiles[$i][0]
                _report("File: " & $sPath  & $aFiles[$i][0] & "|" & $aFiles[$i][1] & "|" & $aFiles[$i][2] _
                         & "|" & $aFiles[$i][3] & "|" & $aFiles[$i][4] & "|" & $aFiles[$i][5], 0, $iDebug)
            Next
            _ArrayConcatenate($aResult, $aFiles, 1)
        EndIf
    EndIf
    $aFolders = _FTP_ListToArrayEx($l_FTPSession, 1)
    If $iDebug = 2 Then _ArrayDisplay($aFolders, 'Ordner: ' & $sPath)
    If IsArray($aFolders) Then
        If $aFolders[0][0] > 0 Then
            For $i = 1 To $aFolders[0][0]
                If $aFolders[$i][0] <> '.' And $aFolders[$i][0] <> '..' Then
                    _FTP_RecListToArrayEx($l_FTPSession, $aResult, $sPath & "/" & $aFolders[$i][0], $bFolders, $iMaxDepth, $iDebug, $b2D, $hLog, $hParent, $iDepth)
                    $aFolders[$i][0] = $sPath & $aFolders[$i][0]
                    _report("File: " & $sPath &  $aFolders[$i][0] & "|" & $aFolders[$i][1] & "|" & $aFolders[$i][2] _
                             & "|" & $aFolders[$i][3] & "|" & $aFolders[$i][4] & "|" & $aFolders[$i][5], 0, $iDebug)
                EndIf
            Next
            For $i = $aFolders[0][0] To 1 Step -1
                If ($aFolders[$i][0] = '.') Or ($aFolders[$i][0] = '..') Then _ArrayDelete($aFolders, $i)
            Next
            If $bFolders Then _ArrayConcatenate($aResult, $aFolders, 1)
        EndIf
    EndIf
    _SumSize($aResult)
EndFunc   ;==>_FTP_RecListToArrayEx
Func _SumSize(ByRef $aFiles)
    $aFiles[0][0] = UBound($aFiles) - 1
    $aFiles[0][1]=0
    For $i=1 To $aFiles[0][0]
        $aFiles[0][1]+=$aFiles[$i][1]
    Next
EndFunc
Func _RealyCancel($hLog = 0, $hParent = 0)
    ;===============================================================================
    ; Autor(s):         AutoBert (www.autoit.de)
    ;                   zur freien Benutzung in eigenen Skripten/EXE'n
    ;                   und auch zur freien Weitergabe unter der Vorgabe,
    ;                   daß diese Funktionsbeschreibung inkl. Autor nicht enfernt wird.
    ;                   Bei EXE'n wäre es nett mich zu erwähnen, ist aber keine Pflicht.
    ; modified:         durch/Grund hier anfügen
    ;===============================================================================
    If MsgBox($MB_YESNO + $MB_ICONQUESTION + $MB_DEFBUTTON2 + $MB_APPLMODAL, 'Download abbrechen', 'Wollen Sie wirklich abbrechen?', 0, $hParent) = $IDYES Then
        _report('Abbruch durch Benutzer', $hLog, 1)
        $bCancel = True
    Else
        $bCancel = False
        $bReturn = False
    EndIf
    Return $bCancel
EndFunc   ;==>_RealyCancel


Func _report($sMsg, $hLog = 0, $iDebug = 0)
    ;===============================================================================
    ; Autor(s):         AutoBert (www.autoit.de)
    ;===============================================================================
    $sMsg=StringReplace($sMsg,'//','/')
    If $hLog <> 0 Then _GUICtrlEdit_AppendText($hLog, $sMsg & @CRLF)
    If $iDebug Then ConsoleWrite($sMsg & @CRLF)
    If $hFileLog > 0 Then _FileWriteLog($hFileLog, $sMsg & @CRLF)
EndFunc   ;==>_report

For downloading, just insert in a loop (instead of ShellExecute) _FTP_FileGet. Other usefull func:

;===============================================================================
; Function Name: _createLocalDirIfNecessary($s_LocalFile)
; Description:: prüft ob für eine Datei die Verzeichnisstruktur besteht, falls nötig wird diese angelegt
; Parameter(s): $s_LocalFile Dateiname für den die Verzeichnisstruktur geprüft werden soll
; Author(s): AutoBert (http://www.autoit.de)
;===============================================================================
Func _createLocalDirIfNecessary($s_LocalFile)
    Local $aLocalPath = StringSplit($s_LocalFile, "\") ;hier lokale Pfad erstellen falls notwendig
    Local $sLocalPath = ""
    For $j = 1 To $aLocalPath[0] - 1
        $sLocalPath &= $aLocalPath[$j] & "\"
        If Not FileExists($sLocalPath) And Not StringInStr($aLocalPath[$j], ":") Then
            ;ConsoleWrite($sLocalPath & @CRLF)
            DirCreate($sLocalPath)
        EndIf
    Next
EndFunc   ;==>_createLocalDirIfNecessary

 

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

×
×
  • Create New...