Jump to content

Recommended Posts

Posted (edited)

Structure of a folder in text, with tree format for a more visually improved format

TreeStructureDir.au3

; https://www.autoitscript.com/forum/topic/211237-treestructuredir
;----------------------------------------------------------------------------------------
; Title...........: TreeStructureDir.au3
; Description.....: Structure of a folder in text, with tree format for a more visually improved format
; AutoIt Version..: 3.3.16.1   Author: ioa747
;----------------------------------------------------------------------------------------
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#include <File.au3>
#include <Array.au3>

Global $sPath = "C:\Program Files (x86)\AutoIt3"

Global $sTxt = TreeStructureDir($sPath)

ConsoleWrite($sPath & @CRLF & $sTxt & @CRLF)

;----------------------------------------------------------------------------------------
Func TreeStructureDir($sDirectoryPath)
    Local $aFolders, $iColCnt, $aSplit, $Result
    $aFolders = _FileListToArrayRec($sDirectoryPath, "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_SORT)
    If @error Then Return SetError(@error, @extended, "")

    $iColCnt = 0
    _ArrayColInsert($aFolders, 1)
    For $i = 1 To $aFolders[0][0]
        $aSplit = StringSplit($aFolders[$i][0], "\")
        If $aSplit[0] > $iColCnt Then $iColCnt = $aSplit[0]
    Next

    For $i = 1 To $iColCnt - 1
        _ArrayColInsert($aFolders, 1)
    Next

    For $i = 1 To $aFolders[0][0]
        $aSplit = StringSplit($aFolders[$i][0], "\")
        For $x = 1 To $aSplit[0]
            $aFolders[$i][$x] = $aSplit[$x]
        Next
    Next

    $iColCnt = UBound($aFolders, 2) - 1

    For $i = 1 To $aFolders[0][0]
        For $x = $iColCnt To 1 Step -1
            If StringLen($aFolders[$i][$x]) > 0 Then
                If $aFolders[$i][$x] = $aFolders[0][$x] Then
                    $aFolders[$i][$x] = "│  "
                Else
                    $aFolders[0][$x] = $aFolders[$i][$x]
                    If $x > 1 Then
                        If $i < $aFolders[0][0] Then
                            If $aFolders[$i][$x - 1] = $aFolders[$i + 1][$x - 1] Then
                                $aFolders[$i][$x] = "├─ " & $aFolders[0][$x]
                            Else
                                $aFolders[$i][$x] = "└─ " & $aFolders[0][$x]
                            EndIf
                        Else
                            $aFolders[$i][$x] = "└─ " & $aFolders[0][$x]
                        EndIf
                    Else
                        $aFolders[$i][$x] = "├─ " & $aFolders[0][$x]
                    EndIf
                EndIf
            Else
                $aFolders[0][$x] = ""
            EndIf
        Next
    Next

    For $i = $aFolders[0][0] To 1 Step -1
        For $x = 1 To $iColCnt
            If $x = 1 And StringLen($aFolders[0][$x]) > 0 Then
                If $aFolders[$i][$x] = "│  " Then $aFolders[$i][$x] = "   "
                If StringLeft($aFolders[$i][$x], 3) = "├─ " Then
                    $aFolders[$i][$x] = StringReplace($aFolders[$i][$x], "├─ ", "└─ ")
                    $aFolders[0][$x] = ""
                EndIf
            EndIf

            If StringLen($aFolders[$i][$x]) = 0 Then $aFolders[0][$x] = ":FIX:"

            If $aFolders[0][$x] = ":FIX:" Then
                If $aFolders[$i][$x] = "│  " Then
                    $aFolders[$i][$x] = "   "
                ElseIf $aFolders[$i][$x] = "" Then
                    ;nothing
                Else
                    $aFolders[0][$x] = ""
                EndIf

                If StringLeft($aFolders[$i][$x], 3) = "├─ " Then
                    $aFolders[$i][$x] = StringReplace($aFolders[$i][$x], "├─ ", "└─ ")
                    $aFolders[0][$x] = ""
                EndIf
            EndIf
        Next
    Next

    For $i = 1 To $aFolders[0][0]
        For $x = 1 To $iColCnt
            $Result &= $aFolders[$i][$x]
        Next
        $Result &= @CRLF
    Next

;~  _ArrayDisplay($aFolders)

    Return BinaryToString(StringToBinary($Result, 4), 4)

EndFunc   ;==>TreeStructureDir
;----------------------------------------------------------------------------------------

 

 Please, every comment is appreciated!
leave your comments and experiences here!
Thank you very much  :)

Edited by ioa747

I know that I know nothing

Posted

just for fun, here's an alternative 'shortened' version ;)

; https://www.autoitscript.com/forum/topic/211237-treestructuredir
;----------------------------------------------------------------------------------------
; Title...........: TreeStructureDir.au3
; Description.....: Structure of a folder in text, with tree format for a more visually improved format
; AutoIt Version..: 3.3.16.1   Author: ioa747
;----------------------------------------------------------------------------------------
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7


Global $sPath = "C:\Program Files (x86)\AutoIt3"

Global $sTxt = TreeStructureDir($sPath)

ConsoleWrite($sPath & @CRLF & $sTxt & @CRLF)

;----------------------------------------------------------------------------------------
Func TreeStructureDir($sDirectoryPath)
    Local $sTree = '', $iPID = Run(@ComSpec & ' /c tree "' & $sDirectoryPath & '" /A', '', @SW_HIDE, 8)
    While ProcessExists($iPID)
        $sTree &= StdoutRead($iPID)
    WEnd
    Return $sTree
EndFunc   ;==>TreeStructureDir
;----------------------------------------------------------------------------------------

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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.
×
×
  • Create New...