_FTP_FileListRekursiv()

  • Servus

    Ich würde gerne alle Dateien mit Größe auf nem Server mit Unterverzeichnissen in nen Array lesen
    Irgendwas stimmt da bei mir aber leider nicht... konnte auch im Forum keine Funktion in der hinsicht finden...

    Spoiler anzeigen
    [autoit]

    #include <ftpEx.au3>
    #include <file.au3>
    #include <array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $Open = _FTP_Open('FTP - Verbindung')
    $Conn = _FTP_Connect($Open, '***', '***', '***')

    [/autoit] [autoit][/autoit] [autoit]

    Global $return[1]
    Global $searchHandle
    $array = _FTP_FileListRekursiv('/html/')
    _ArrayDisplay ( $array )

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _FTP_FileListRekursiv($current)
    _FTP_DirSetCurrent ( $Conn,$current )
    $file = _FTP_FindFileFirst( $Conn, $current ,$searchHandle )
    If $file[1] == 128 Then
    _ArrayAdd ( $return, $current&$file[10] )
    EndIf
    While 1
    Local $file = _FTP_FindFileNext($searchHandle)
    If @error Then ExitLoop
    If $file[1] == 128 Then; wenns eine Datei ist
    _ArrayAdd ( $return, $current&$file[10] )
    EndIf
    If $file[1] == 16 Then ; wenns ein Ordner ist..
    _FTP_FileListRekursiv($current&$file[10]&'/')
    EndIf
    WEnd
    _FTP_FindFileClose($searchHandle)
    $return[0] = UBound ( $return ) -1
    Return $return
    EndFunc

    [/autoit]

    Danke schonmal für Hilfe

    MFG chris :D

  • Hi,
    hab dir mal schnell was gebaut:

    Spoiler anzeigen
    [autoit]

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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $Open = _FTP_Open("Hallo123")
    $hCon = _FTP_Connect($Open, "ftp.csx.cam.ac.uk", "", "")

    [/autoit] [autoit][/autoit] [autoit]

    Global $aArray[1]

    [/autoit] [autoit][/autoit] [autoit]

    Recrusive("")
    _ArrayDisplay($aArray)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func Recrusive($sPath)
    Local $aFiles, $aDirs
    _FTP_DirSetCurrent($hCon, $sPath)

    [/autoit] [autoit][/autoit] [autoit]

    $aFiles = _Ftp_ListToArray($hCon, 2)
    If IsArray($aDirs) Then
    For $x = 1 To $aFiles[0]
    _ArrayAdd($aArray, $aFiles[$x])
    Next
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $aDirs = _Ftp_ListToArray($hCon, 1)
    If IsArray($aDirs) Then
    For $x = 1 To $aDirs[0]
    ConsoleWrite($sPath & "/" & $aDirs[$x] & @CRLF)
    Recrusive($sPath & "/" & $aDirs[$x])
    Next
    EndIf
    EndFunc ;==>Recrusive

    [/autoit]

    //Edit: Hab nen keleinen Fehler drinne gehabt. Berichtigt

  • @Sprenger

    Ich glaube du hast einen kleinen Fehler ;)

    [autoit]

    $aFiles = _Ftp_ListToArray($hCon, 2)
    If IsArray($aFiles) Then
    For $x = 1 To $aFiles[0]
    _ArrayAdd($aArray, $aFiles[$x])
    Next
    EndIf

    [/autoit][autoit][/autoit][autoit][/autoit]
  • Ja hab ich gesehn. Habs gefixt.

    Muss dier erste If-Abfrage nicht anderst lauten?

    Spoiler anzeigen
    [autoit]

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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $Open = _FTP_Open("Hallo123")
    $hCon = _FTP_Connect($Open, "ftp.csx.cam.ac.uk", "", "")

    [/autoit] [autoit][/autoit] [autoit]

    Global $aArray[1]

    [/autoit] [autoit][/autoit] [autoit]

    Recrusive("")
    _ArrayDisplay($aArray)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func Recrusive($sPath)
    Local $aFiles, $aDirs
    _FTP_DirSetCurrent($hCon, $sPath)

    [/autoit] [autoit][/autoit] [autoit]

    $aFiles = _Ftp_ListToArray($hCon, 2)
    If IsArray($aFiles) Then
    For $x = 1 To $aFiles[0]
    _ArrayAdd($aArray, $aFiles[$x])
    Next
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $aDirs = _Ftp_ListToArray($hCon, 1)
    If IsArray($aDirs) Then
    For $x = 1 To $aDirs[0]
    ConsoleWrite($sPath & "/" & $aDirs[$x] & @CRLF)
    Recrusive($sPath & "/" & $aDirs[$x])
    Next
    EndIf
    EndFunc ;==>Recrusive

    [/autoit]
  • Hi,
    sry mein FF hat gesponnen. Hier ist der funktionierende Code:

    Spoiler anzeigen
    [autoit]

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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $Open = _FTP_Open("Hallo123")
    $hCon = _FTP_Connect($Open, "ftp.csx.cam.ac.uk", "", "")

    [/autoit] [autoit][/autoit] [autoit]

    Global $aArray[1]

    [/autoit] [autoit][/autoit] [autoit]

    Recrusive("")
    _ArrayDisplay($aArray)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func Recrusive($sPath)
    Local $aFiles, $aDirs
    _FTP_DirSetCurrent($hCon, $sPath)

    [/autoit] [autoit][/autoit] [autoit]

    $aFiles = _Ftp_ListToArray($hCon, 2)
    If IsArray($aFiles) Then
    For $x = 1 To $aFiles[0]
    _ArrayAdd($aArray, $sPath & "/" & $aFiles[$x])
    Next
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $aDirs = _Ftp_ListToArray($hCon, 1)
    If IsArray($aDirs) Then
    For $x = 1 To $aDirs[0]
    ConsoleWrite($sPath & "/" & $aDirs[$x] & @CRLF)
    Recrusive($sPath & "/" & $aDirs[$x])
    Next
    EndIf
    EndFunc ;==>Recrusive

    [/autoit]
  • auch wenn alles schon vorbei und geklärt ist - der vollständigkeit halber meine Lösung an der ich gerade gesessen habe:

    Spoiler anzeigen
    [autoit]

    #include <ftpEx.au3>
    #include <array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $server = 'ftp.csx.cam.ac.uk'
    $username = ''
    $pass = ''

    [/autoit] [autoit][/autoit] [autoit]

    $hFTP = _FTP_Open('FTP_open')
    $hFTPCon = _FTP_Connect($hFTP, $server, $username, $pass)

    [/autoit] [autoit][/autoit] [autoit]

    $aFileList = _FTP_FileList($hFTPCon, "pub/software/antivirus/")
    _ArrayDisplay($aFileList)

    [/autoit] [autoit][/autoit] [autoit]

    Func _FTP_FileList(Const $hConn, $sPath = "")
    ;by AspirinJunkie
    Local $cSQueue = ObjCreate("System.Collections.Queue")
    Local $oRetDict = ObjCreate("Scripting.Dictionary")
    Local $hFFFF, $aNext, $sCurrentPath

    [/autoit] [autoit][/autoit] [autoit]

    If StringRight($sPath, 1) = "/" Then $sPath = StringTrimRight($sPath, 1)
    $cSQueue.Enqueue($sPath)

    [/autoit] [autoit][/autoit] [autoit]

    While $cSQueue.Count > 0
    $sCurrentPath = $cSQueue.Dequeue

    [/autoit] [autoit][/autoit] [autoit]

    $aNext = _FTP_FindFileFirst($hConn, $sCurrentPath, $hFFFF)
    If @error Then
    If $oRetDict.Count > 0 Then ContinueLoop
    Local $Ret[1] = [-1]
    Return SetError(1, 0, $Ret)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If BitAND($aNext[1], 16) Then $cSQueue.Enqueue($sCurrentPath & '/' & $aNext[10])
    $oRetDict.Add($sCurrentPath & '/' & $aNext[10], 0)

    [/autoit] [autoit][/autoit] [autoit]

    Do
    $aNext = _FTP_FindFileNext($hFFFF)
    If @error Then ExitLoop
    If BitAND($aNext[1], 16) Then $cSQueue.Enqueue($sCurrentPath & '/' & $aNext[10])
    $oRetDict.Add($sCurrentPath & '/' & $aNext[10], 0)
    Until 0
    _FTP_FindFileClose($hFFFF)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    If $oRetDict.Count > 0 Then
    Return $oRetDict.Keys
    Else
    Local $Ret[1] = [0]
    Return SetError(2, 0, $Ret)
    EndIf
    EndFunc ;==>_FTP_FileListRekursiv

    [/autoit]

    Einmal editiert, zuletzt von AspirinJunkie (28. Dezember 2010 um 00:19)


  • Hatte dir auch in der SB ca. 20-30 min nach deiner Frage letzte Nacht den Hinweis gegeben,

    mfg autoBert

    Mist dann hab ich das nicht mehr gelesen... naja ich denke das sollte man der FTPEx.au3 dann auch hinzufügen...
    Am besten bei den _FTP_ListToArray... als optional für rekursiv nen True oder False
    Aber Danke trotzdem...

    MFG chris :D

  • Huhu

    Hier falls Ihr es noch gebrauchen könnt meine Version mit 2D Array

    Spoiler anzeigen
    [autoit]

    #include <ftpEx.au3>
    #include <file.au3>
    #include <array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $server = 'ftp.csx.cam.ac.uk'
    $username = ''
    $pass = ''

    [/autoit] [autoit][/autoit] [autoit]

    $hFTP = _FTP_Open('FTP_open')
    $hCon = _FTP_Connect($hFTP, $server, $username, $pass)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Global $aArray[1][2]
    _FTP_RecrusiveFileList('/pub/software/antivirus/datfiles_old')
    _ArrayDisplay ( $aArray )

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _FTP_RecrusiveFileList($sPath)
    Local $aFiles, $aDirs
    _FTP_DirSetCurrent($hCon, $sPath)

    [/autoit] [autoit][/autoit] [autoit]

    $aFiles = _Ftp_ListToArray2D($hCon, 2)
    If IsArray($aFiles) Then
    For $x = 1 To $aFiles[0][0]
    $aArray[0][1] += $aFiles[$x][1]
    ConsoleWrite($sPath & "/" & $aFiles[$x][0] & @CRLF)
    _Array2DAdd($aArray, $sPath & "/" & $aFiles[$x][0]&'|'& $aFiles[$x][1])
    Next
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $aDirs = _Ftp_ListToArray($hCon, 1)
    If IsArray($aDirs) Then
    For $x = 1 To $aDirs[0]
    ;~ ConsoleWrite($sPath & "/" & $aDirs[$x] & @CRLF)
    _FTP_RecrusiveFileList($sPath & "/" & $aDirs[$x])
    Next
    EndIf
    $aArray[0][0] = UBound($aArray)-1

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>Recrusive

    [/autoit] [autoit][/autoit] [autoit]

    Func _Array2DAdd(ByRef $avArray, $sValue = '')
    If (Not IsArray($avArray)) Then
    SetError(1)
    Return 0
    EndIf
    Local $UBound2nd = UBound($avArray, 2)
    If @error = 2 Then
    ReDim $avArray[UBound($avArray) + 1]
    $avArray[UBound($avArray) - 1] = $sValue
    Else
    Local $arValue
    ReDim $avArray[UBound($avArray) + 1][$UBound2nd]
    If $sValue = '' Then
    For $i = 0 To $UBound2nd - 2
    $sValue &= '|'
    Next
    EndIf
    $arValue = StringSplit($sValue, '|')
    If $arValue[0] <> $UBound2nd Then
    SetError(2)
    Return 0
    EndIf
    For $i = 0 To $UBound2nd - 1
    $avArray[UBound($avArray) - 1][$i] = $arValue[$i + 1]
    Next
    EndIf
    Return -1
    EndFunc

    [/autoit]

    MFG chris :D

  • Hab das mit der Dateigröße überlesen - daher das ganze nochmal angepasst und übersichtlicher in der Ausgabe gemacht:

    Spoiler anzeigen
    [autoit]

    #include <ftpEx.au3>
    #include <array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $server = 'ftp.csx.cam.ac.uk'
    $username = ''
    $pass = ''

    [/autoit] [autoit][/autoit] [autoit]

    $hFTP = _FTP_Open('FTP_open')
    $hFTPCon = _FTP_Connect($hFTP, $server, $username, $pass)

    [/autoit] [autoit][/autoit] [autoit]

    $aFileList = _FTP_FileList($hFTPCon, "pub/software/antivirus/datfiles_old")
    _ArrayDisplay($aFileList)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ======================================================================================
    ; Name ..........: _FTP_FileList()
    ; Description ...: Returns a List of all Files include SubDirs of a FTP-Path with Size
    ; Syntax ........: _FTP_FileList(ByRef $hConn[, $sPath = ""])
    ; Parameters ....: ByRef $hConn - as returned by _FTP_Connect().
    ; $sPath - [optional] path to be used when searching the files (default:"")
    ; Return values .: Success: A 2D Array with [Path, FileName,FileSize], $Array[0,0] = Number of Elements
    ; Failure: Sets @error and $Array[0,0] = -1
    ; Author ........: AspirinJunkie
    ; Modified ......: 28.12.2010
    ; Remarks .......: Needs .Net for System.Collections.Queue
    ; Example .......: Yes
    ; #include <ftpEx.au3>
    ; #include <array.au3>
    ; $server = 'ftp.csx.cam.ac.uk'
    ; $username = ''
    ; $pass = ''
    ; $hFTP = _FTP_Open('FTP_open')
    ; $hFTPCon = _FTP_Connect($hFTP, $server, $username, $pass)
    ; $aFileList = _FTP_FileList($hFTPCon, "pub/software/antivirus/datfiles_old")
    ; _ArrayDisplay($aFileList)
    ; =================================================================================================
    Func _FTP_FileList(ByRef $hConn, $sPath = "")
    Local $cSQueue = ObjCreate("System.Collections.Queue")
    Local $oRetDict = ObjCreate("Scripting.Dictionary")
    Local $hFFFF, $aNext, $sCurrentPath
    If StringRight($sPath, 1) = "/" Then $sPath = StringTrimRight($sPath, 1)
    $cSQueue.Enqueue($sPath)

    [/autoit] [autoit][/autoit] [autoit]

    While $cSQueue.Count > 0
    $sCurrentPath = $cSQueue.Dequeue

    [/autoit] [autoit][/autoit] [autoit]

    $aNext = _FTP_FindFileFirst($hConn, $sCurrentPath, $hFFFF)
    If @error Then
    If $oRetDict.Count > 0 Then ContinueLoop
    Local $Ret[1][3] = [[-1]]
    Return SetError(1, 0, $Ret)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If BitAND($aNext[1], 16) Then
    $cSQueue.Enqueue($sCurrentPath & '/' & $aNext[10])
    $oRetDict.Add($sCurrentPath & '\\' & $aNext[10] & '/' & "\\0", 0)
    Else
    $oRetDict.Add($sCurrentPath & '\\' & $aNext[10] & "\\" & BitOR(BitShift($aNext[8], -8), $aNext[9]), 0)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Do
    $aNext = _FTP_FindFileNext($hFFFF)
    If @error Then ExitLoop
    If BitAND($aNext[1], 16) Then
    $cSQueue.Enqueue($sCurrentPath & '/' & $aNext[10])
    $oRetDict.Add($sCurrentPath & '\\' & $aNext[10] & '/' & "\\0", 0)
    Else
    $oRetDict.Add($sCurrentPath & '\\' & $aNext[10] & "\\" & BitOR(BitShift($aNext[8], -8), $aNext[9]), 0)
    EndIf
    Until 0
    _FTP_FindFileClose($hFFFF)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    If $oRetDict.Count > 0 Then
    Local $aRet[$oRetDict.Count + 1][3] = [[$oRetDict.Count, "Dateiname", "Dateigröße [Bytes]"]]
    Local $iC = 1, $aT
    For $i In $oRetDict.Keys
    $aT = StringSplit($i, "\\", 3)
    $aRet[$iC][0] = $aT[0]
    $aRet[$iC][1] = $aT[1]
    $aRet[$iC][2] = Number($aT[2])
    $iC += 1
    Next
    Return $aRet
    Else
    Local $Ret[1][3] = [[-1]]
    Return SetError(2, 0, $Ret)
    EndIf
    EndFunc ;==>_FTP_FileList

    [/autoit]

    @ChrisAttack
    So wie du es jetzt mit dem globalen Array machst kann man die Funktion nur 1x in einem Skript aufrufen da sonst die Daten einfach immer wieder nur an das globale Array angehängt werden.