Jump to content

Pulling a directory listing from a public ftp

Recommended Posts


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


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 post
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)



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 post
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)


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

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to post
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)
            _Report('Ftpsitzung geschlossen', $hLog, 1)
            _Report('Internetsitzung geschlossen', $hLog, 1)
            $l_InternetSession = -99
            $l_FTPSession = -99
            _Report('Verbindung mit ' & $sServer & ' fehlgeschlagen: ' & $errFTP, $hLog, 1)
        _Report('öffnen der Internetsitzung fehlgeschlagen: ' & $errOpen, $hLog, 1)
    _Report('======================= End of Loop =======================')
    _FileWriteLog('FTPLoop.txt', $aFiles[0][0]&'|'&$aFiles[0][1] & @CRLF)


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
    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'
    $iDepth += 1
    _FTP_DirSetCurrent($l_FTPSession, $sPath)
    If @error Then
        _report("Remoteverzeichnis: " & $sPath & " existiert nicht!", 0, $iDebug)
        $sPath = _FTP_DirGetCurrent($l_FTPSession) & '/'
    $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)
            _ArrayConcatenate($aResult, $aFiles, 1)
    $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)
            For $i = $aFolders[0][0] To 1 Step -1
                If ($aFolders[$i][0] = '.') Or ($aFolders[$i][0] = '..') Then _ArrayDelete($aFolders, $i)
            If $bFolders Then _ArrayConcatenate($aResult, $aFolders, 1)
EndFunc   ;==>_FTP_RecListToArrayEx
Func _SumSize(ByRef $aFiles)
    $aFiles[0][0] = UBound($aFiles) - 1
    For $i=1 To $aFiles[0][0]
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
        $bCancel = False
        $bReturn = False
    Return $bCancel
EndFunc   ;==>_RealyCancel

Func _report($sMsg, $hLog = 0, $iDebug = 0)
    ; Autor(s):         AutoBert (www.autoit.de)
    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)
EndFunc   ;==>_createLocalDirIfNecessary


Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By wysocki
      I have a smartphone and I use it to access my email. However, when composing an email on it I have a problem. My list of phone contacts on the phone is very different from my list of email contacts in my Thunderbird desktop app.  I use my Gmail address book to store primarily phone contacts, and I use Thunderbird for my list of email contacts. I wanted a way to get my Thunderbird contact list onto my smartphone to be able to compose emails to addresses in that list. Here's my solution.
      I wrote a script to export my Thunderbird Personal Address Book to a csv file. It then reads that file and re-writes it with html wrappers around the data to make it into a nicely formatted web page. It then uploads the htm file to my website. On my smartphone, I created a shortcut to the file's URL and whenever I click it, I get the list displayed. Each contact shows name and email address along with a COPY button that will put the address into the clipboard. Then in my email client, I can easily paste that address into it. Alternatively, clicking on the actual email link will open a new message dialog in your email client with that address already entered.
      To use the app, all you need to do is use Thunderbird and have a webserver available. You'll need to download the FTPEX.AU3 file from this website and make a few changes to some constants around line 17 for FTP login info, etc.
    • By Dent
      The following function successfully connects to and uploads a local text file named user.dat
      I have checked the data being written to the text file when it is created locally using a MsgBox and it appears exactly how it should be written to the file. If I comment out the FileDelete and go and open the file locally it is as expected.
      However when I download the file from the FTP server and open it up the text that should be at the end of the file is missing. With each subsequent run more characters are missing.
      I added the Sleep(5000) in case the function was closing the FTP connection too quickly before the file could be fully written but it makes no difference. The user.dat file is (should be) approximately 100 bytes so it is tiny.
      Any idea why this is happening?
      Func UpdateUserData() ; Upload the modified user.dat file Local $hOpen = _FTP_Open("myftp") Local $hConn = _FTP_Connect($hOpen, "my.ftp.server", "user", "pass", 1, 0, 1, 2) If @error Then MsgBox(16, "Error", "Connection failed" & @CRLF & @CRLF & "Please contact support") _FTP_Close($hConn) _FTP_Close($hOpen) Exit EndIf _FTP_FilePut($hConn, @TempDir & "\user.dat", "user.dat") ; Upload the new user.dat file Sleep(5000) If @error <> 0 Then MsgBox(16, "Error", "Couldn't transmit data" & @CRLF & @CRLF & "Please contact support") _FTP_Close($hConn) _FTP_Close($hOpen) Exit EndIf _FTP_Close($hConn) _FTP_Close($hOpen) FileDelete(@TempDir & "\user.dat") EndFunc  
    • By Raywando
      This is my first post. So I’ve worked on a script for a while and I’m planning to publish it but the problem is that it connects to an FTP server at some point, and as you probably know FTP credentials are easily captured by a MITM attack or Wireshark (not sure if Wireshark does). So I thought if i can detect data capturing in the user’s network the script would stop. Any idea?.
      If there’s another workaround I’m happy to hear it. 
    • By Jemboy
      Ones(some times twice) a month I get an e-mail with zip file, which has price updates from a supplier.
      I have to upload the file to an FTP to get it processed.
      When uploading the file, it will get "timestamped" with the time and date at which time the file was uploaded.
      Normally this is fine, because I mostly upload the file the same day.
      Sometimes it may take 1 or 2 days before I can upload the file.
      For historical purpose, I would like to have the file timestamped with the original date.
      I have tried using:  _FTP_Command ( $hFTPSession, "MFCT YYYYMMDDHHMMSS path") 
      however this command does not work or change the timestamp as I expected.
      Does anyone now a way how I can change the timestamp of a FTP-file?
    • By Sally1801
      i need help in deleting files from a server. The function "_FTP_DirDelete" only deletes a directory when its empty. I need to delete a non empty directory. I also can not delete the files in this directory first, because the files are PAG and DIR files in a .DAV directory and FTPEx.au3 doesnt like a directory starting with a ".".
      Any ideas?
      Thx, Sally
  • Create New...