Verzeichnisstruktur in Array einlesen

  • Hallo,
    ich möchte ein Verzeichnis samt aller Datei- und Ordnernamen in ein Array einlesen. Habe dazu folgendes gefunden:

    Spoiler anzeigen
    [autoit]

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

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

    $Anzahl = 1001 ; Size of the Array
    Global $filesarr[$Anzahl][$Anzahl][3]; 1.[]: Nummer des Verzeichnisses = number of the folder
    ; 2.[]: Nummer der Datei = number of the file in that folder
    ; 3.[]: 0=DateiName 1=relativer Odner 2=komplatter Pfad = 0=filename 1=relative_path 2=absolute_path
    ; $filesarr[0][0][0] = Anzahl aller Ordner = total number of all folders
    ; $filesarr[0][0][1] = Absolute Anzahl an Dateien = total number of all files
    ; $filesarr[x][0][0] = Anzahl der Unterordner in Ordner x = number of subfolders in folder x
    ; $filesarr[x][0][1] = Anzahl der Dateien im Ordner x = number of files in folder x
    ; $filesarr[x][0][2] = Hilfesvariabel für den Rücksprung = only help to find back to the richt folder
    For $i=0 to $Anzahl-1
    $filesarr[$i][0][0] = 0 ; Anzahl der Unterordner auf 0 setzen = all number of subfolders = 0
    $filesarr[$i][0][1] = 0 ; Anzahl der Dateien im Verzeichnis auf 0 setzen = all number of files = 0
    Next
    Global $search[$Anzahl]
    $folderID = 1
    $filesarr[0][0][0] = 1

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

    Global $choosefolder = FileSelectFolder("Zu bearbeitender Ordner:","") ; choose the folder

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

    Read_Folder_2_Array($choosefolder)

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

    show_all_FolderArray()

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

    show_Summery()

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

    Exit

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

    Func Read_Folder_2_Array($path)

    ;Local $path,$info,$file,$folder
    ;MsgBox(0,"Loop-Info","current ID = " & $folderID & @CRLF & "total ID = " & $filesarr[0][0][0])

    ; Grundannahme, daß das Verzeichnis leer ist = presumption that the folder is empty
    $file = "(leeres Verzeichnis)" ; = (empty folder)
    $folder = StringTrimLeft(StringReplace($path,$choosefolder,""),1)
    $filesarr[$folderID][1][0] = $file ; DateiName = "(leeres Verzeichnis)"
    $filesarr[$folderID][1][1] = $folder ; relativer Odner
    $filesarr[$folderID][1][2] = $path & "\" & $file ; kompletter Pfad mit "(leeres Verzeichnis)"

    $search[$folderID] = FileFindFirstFile($path & "\*.*")
    If $search[$folderID] <> -1 Then
    While 1
    ;MsgBox(0,"$folderID","$folderID = " & $folderID)
    $file = FileFindNextFile($search[$folderID])
    If @error Then
    ;MsgBox(0,"Dateien alle ausgelesen","$folderID = " & $folderID & @CRLF & "$file = " & $file)
    ExitLoop
    EndIf
    ;MsgBox(0,"File-Info","$folderID = " & $folderID & @CRLF & "$file = " & $file)
    If $file <> "." And $file <> ".." Then
    If StringInStr(FileGetAttrib($path & "\" & $file),"D") Then
    $filesarr[0][0][0] = $filesarr[0][0][0] + 1 ; Anzahl alle Ordner um 1 erhöhen
    $filesarr[$folderID][0][0] = $filesarr[$folderID][0][0] + 1 ; Anzahl der gefundenen Unterordner in diesem Ordner um 1 erhöhen
    $filesarr[ $filesarr[0][0][0] ][0][2] = $folderID ; Rücksprung festhalten
    $folderID = $filesarr[0][0][0]
    Read_Folder_2_Array($path & "\" & $file)
    $folderID = $filesarr[$folderID][0][2] ; Rücksprung in das vorher bearbeitete Verzeichnis
    Else
    $info = StringReplace($path & "\" & $file,$choosefolder,"")
    $folder = StringTrimLeft(StringReplace($path,$choosefolder,""),1) ; relativer Odner definieren
    If StringInStr($info,"\",0,-1) > 1 Then
    $singlefile = StringTrimLeft($info,StringLen($folder) + 1)
    Else
    $singlefile = $info
    EndIf
    $singlefile = StringTrimLeft($singlefile,1)
    $filesarr[0][0][1] = $filesarr[0][0][1] + 1 ; Absolute Anzahl an Dateien
    $filesarr[$folderID][0][1] = $filesarr[$folderID][0][1] + 1 ; Anzahl der Dateien in diesem Verzeichnis
    $filesarr[$folderID][$filesarr[$folderID][0][1]][0] = $singlefile ; DateiName
    $filesarr[$folderID][$filesarr[$folderID][0][1]][1] = $folder ; relativer Odner
    $filesarr[$folderID][$filesarr[$folderID][0][1]][2] = $path & "\" & $file; kompletter Pfad
    EndIf
    EndIf
    WEnd
    ;MsgBox(0,"Schleife schliessen","$folderID = " & $folderID & @CRLF & "$filesarr[0][0][0] = " & $filesarr[0][0][0])
    Else
    ;MsgBox(0,"Leeres Verzeichnis","$folderID = " & $folderID & @CRLF & "$search[$folderID] = " & $search[$folderID])
    EndIf
    FileClose($search[$folderID])

    EndFunc

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

    Func show_all_FolderArray()

    For $o=1 to $filesarr[0][0][0]
    $OrdnerInhalt = "Ordner " & $o & @CRLF & "Ordner : " & $filesarr[$o][1][1] & @CRLF & "Pfad : " & $filesarr[$o][1][2] & @CRLF & @CRLF & " enthält die folgenden " & $filesarr[$o][0][1] & " Dateien: " & @CRLF
    For $i=1 to $filesarr[$o][0][1]
    ;MsgBox(0,"Info","Ordner = " & $o & " Datei = " & $i & @CRLF & "$filesarr["&$o&"]["&$i&"][0] = " & $filesarr[$o][$i][0] & @CRLF & "$filesarr["&$o&"]["&$i&"][1] = " & $filesarr[$o][$i][1] & @CRLF & "$filesarr["&$o&"]["&$i&"][2] = " & $filesarr[$o][$i][2])
    $OrdnerInhalt = $OrdnerInhalt & $i & " = " & $filesarr[$o][$i][0] & @CRLF
    Next
    MsgBox(0,"Liste",$OrdnerInhalt)
    Next

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

    EndFunc

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

    Func show_Summery()

    $Info = "Anzahl der Ordner : " & $filesarr[0][0][0] & @CRLF & "Anzahl der Dateien : " & $filesarr[0][0][1] & @CRLF & @CRLF
    For $o=1 to $filesarr[0][0][0]
    $Info = $Info & "Ordner " & $o & " hat " & $filesarr[$o][0][1] & " Dateien und " &$filesarr[$o][0][0] & " Unterordner (" & $filesarr[$o][1][1] & ")"& @CRLF
    Next
    MsgBox(0,"Gesamt-Info",$Info)

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

    EndFunc

    [/autoit]


    Das scheint mir doch bischen kompliziert und daher wollte ich fragen, ob es nicht auch eine einfachere "3-Zeilen-Lösung" gibt?

    Einmal editiert, zuletzt von Scritch (16. Mai 2011 um 10:12)