Jump to content

read files + foldes + subfolders into array


sabine-uwe
 Share

Recommended Posts

Hello

I was searching for a script with gives me all information about a folder (his files, his subfolders and all files in the subfolder).

I found some good idears, but not exactly what I needed.

Therefore I used a script from Holger to modifiy it.

(Original Topic: Simple Selfextracter, using Autoit3-FileInstall)

With this script now you can choose a start-folder -> than you will get an array with

- the number of folders : [0][0][0]

- the number of files in the whole array : [0][0][1]

- the number of subfolders of thefolder (x) : [x][0][0]

- the number of files of the folder (x) : [x][0][1]

- file (y) in this folder (x) : [x][y][0]

- the relative path to this folder (x) : [x][y][1]

- the whole path to the file (y) : [x][y][2]

-> and this also for every subfolder.

Maybe somebody can use it - or if you find a mistake I would appreciate your advice or correction. :)

Maybe it can also help for the "TreeView browsing"-Project ?

Regards

Uwe

P.S.: Sorry for the german remarks and names in the code, but I was to lazy to translate all stuff -> should be understandable

$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

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

Read_Folder_2_Array($choosefolder)

show_all_FolderArray()

show_Summery()

Exit



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

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

EndFunc

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)

EndFunc
Edited by sabine-uwe
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...