Jump to content
Carm01

Pulling a directory listing from a public ftp

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

 

 

 

Share this post


Link to post
Share on other sites

Try using _FTP_ListToArray and it's associated functions to get the list of files and folders :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


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)

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

Share this post


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)

_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


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


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

 

Share this post


Link to post
Share on other sites

@TheDcoder I appreciate the explanation as it helps explain this a lot more thoroughly. I realized that the ftp did not exist a little later on.

I would hope that they update their documentation with an example like yours to help some of noobs

Once again I appreciate the assistance

and thank You to @AutoBert for replying as well

Share this post


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

  • Similar Content

    • 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
      Hello,
      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
      Hi,
      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
      Hello,
      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
    • By CharlieH
      Hi,
       
      I have written a small script to read a file from an FTP server and check its contents. This can be re-run by the press of a button, however, if the file is deleted from the ftp server between runs (with the .exe still live), the file is still "read" and written locally, which then passes the check.
       
      If I run it initially without the file, it correctly fails and pops up my error message, but if the file is then added, it then seemingly gets cached (or similar) so that the app then always reports a success.
       
      The below code snippet is just the function run when the "test" button is pressed. It includes a load of debug message boxes, and from that I think I've gathered a few (possibly) interesting/relevant things:
      $Open and $Conn are 8 byte values, which increments (not by 1) when the FTP connection is not closed, but if the connection is closed, they stick at the value (presumably windows can re-use that session id if it's been closed e.g:
      1st run (file not present) $Open -> 0x00CC0004 $Conn -> 0x00CC0008
      2nd run (file not present) $Open -> 0x00CC0010 $Conn -> 0x00CC0014
      3rd run (file now present) $Open -> 0x00CC001C $Conn -> 0x00CC0020
      4th run (file still present) $Open -> 0x00CC001C $Conn -> 0x00CC0020
      $Ftp = _FTP_FileGet.... returns a 1 when file not present (in a run after it was present) and "test_transfer.txt" does get created and does contain the correct string This is the key bit I dont understand, I dont know how/where it is getting the data to write this file when it literally no longer exists on the target FTP server...
      resetting every variable used in the function each time it's run does work (in that they become 0), but it doesn't affect the putcome I had thought perhaps some key values were being stored in the variables, but this doesn't seem to be the case
       
       
      Is there any concept of clearing a cache when closing an ftp session? Or deleting any unknown temporary files windows might make?
       
      Thanks all
       
      Func Transfer() Local $connected = 0 $Ftpp = 0 ;Trying to reset these every time function is called $file = 0 $Open = 0 $Conn = 0 $Ftpc = 0 ;Make a new "connecting..." window so that the user has feedback that a transfer is attempting to take place ;Otherwise it just runs in the background and there's no indication its doing anything $connection_window = GUICreate ("Ethernet Switch Test" , 300 , 160 , -1 , -1 , -1 , -1 , 0) GUISetBkColor(0xFFFFFF) GUISetFont(10 * _GDIPlus_GraphicsGetDPIRatio()[0], 400, Default, "Sans Serif") $connecting_label = GUICtrlCreateLabel("Connecting to board...", 0, 25, 300, -1, $SS_Center, "") GUISetState(@SW_SHOW, $connection_window) Sleep(100) ;Connect ;MsgBox(0, "DEBUG", "1" & $Conn) While $connected = 0 $Open = _FTP_Open($count) ;MsgBox(0, "DEBUG", "open " & $Open) $Conn = _FTP_Connect($Open, $server, $username, $password) ;MsgBox(0, "DEBUG", "2" & $Conn) If $Conn = 0 then Local $retry = Msgbox(65, 'FTP Transfer', 'Connection failed' & @CRLF & "Retry?") If $retry = 2 Then MsgBox(0, "FTP Transfer", "Operation aborted") GUISetState(@SW_HIDE, $connection_window) Return EndIf Else $connected_label = GUICtrlCreateLabel("Connected!", 0, 45, 300, -1, $SS_Center, "") $connected = 1 MsgBox(0, "DEBUG", "3" & $connected) Sleep(100) endIf WEnd $transfering_label = GUICtrlCreateLabel("Reading file....", 0, 45, 300, -1, $SS_Center, "") ;Read file from server ;MsgBox(0, "DEBUG", "5" & $Ftpp) ;MsgBox(0, "DEBUG", "flie " & $file) $Ftpp = _FTP_FileGet($Conn, 'test/test.txt', 'test_transfer.txt') ;MsgBox(0, "DEBUG", "6" & $Ftpp) If ($Ftpp) then $transfered_label = GUICtrlCreateLabel("Transfered, checking...", 0, 65, 300, -1, $SS_Center, "") ;MsgBox(0, "DEBUG", "flie " & $file) $file = FileRead("test_transfer.txt") ;MsgBox(0, "DEBUG", "flie " & $file) If Not StringInStr($file, 'this is a test string 12345') Then MsgBox(0, "File check", "Received file incorrect, test failed!") GUISetState(@SW_HIDE, $connection_window) Return Else $tested_label = GUICtrlCreateLabel("Tested and Passed!", 0, 85, 300, -1, $SS_Center, "") $Ftpc = _FTP_Close($Open) ;MsgBox(0, "DEBUG", "close" & $Ftpc) ;$count = $count+1 $ok_button = GUICtrlCreateButton("OK", 125, 105, 50, -1) While 1 Local $pressed = GUIGetMsg() If ($pressed = $ok_button) Then FileDelete("test_transfer.txt") GUISetState(@SW_HIDE, $connection_window) ;$connection_window = 0 Return EndIf WEnd EndIf Else MsgBox(0, "Transfer", "Could not read file " & @error) GUISetState(@SW_HIDE, $connection_window) Return EndIf EndFunc  
×
×
  • Create New...