Jump to content

Recommended Posts

Posted (edited)

Please help me sort the directory tree by level.
I want to sort the folders in order: 6-> 5> 4> 3> 2> 1

Thanks for all the help and suggestions!

computer-network-file-folder-organization-structure-flowchart-vector-graphic-sharp-professional-tree-concept-traditional-yellow-106778812.thumb.jpg.72de129fd00585f89adde06bf0b3db21.jpg

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

Global Const $sRemotePath = '/user/trong/sync'
Global Const $sLocalPath = @MyDocumentsDir

_Sequential($sLocalPath, $sRemotePath)

Func _Sequential($iLocalPath, $iRemotePath)
    $iLocalPath = StringReplace($iLocalPath, "/", "\")
    $iRemotePath = StringReplace($iRemotePath, "\", "/")
    If (StringRight($iLocalPath, 1) = "\") Then $iLocalPath = StringTrimRight($iLocalPath, 1)
    ConsoleWrite("> From: " & $iLocalPath & @CRLF)
    ConsoleWrite(">   TO: " & $iRemotePath & @CRLF)
    Local $aDirList = _FileListToArrayRec($iLocalPath, '*', $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
    If (Not @error) And IsArray($aDirList) Then
        For $d = 1 To $aDirList[0]
            ConsoleWrite("! Current Path (" & $d & '/' & $aDirList[0] & '): ' & $aDirList[$d] & @CRLF)
            Local $aFileList = _FileListToArray($aDirList[$d], '*', $FLTA_FILES, False)
            If (Not @error) And IsArray($aFileList) Then
                For $f = 1 To $aFileList[0]
                    ConsoleWrite("+ File: (" & $f & '/' & $aFileList[0] & ') ON Path ' & $d & '/' & $aDirList[0] & @CRLF)
                    Local $iPathName = StringReplace($aDirList[$d], $iLocalPath & '\', '')
                    Local $iFileName = _SplitPath($aFileList[$f], 5)
                    Local $iRemoteDir = StringReplace($iRemotePath & '/' & $iPathName, '\', '/')
                    Local $iRemoteFile = StringReplace($iRemoteDir & '/' & $iFileName, '\', '/')
                    Local $iLocalFile = $aDirList[$d] & '\' & $iFileName

                    ConsoleWrite("- Path Name  : " & $iPathName & @CRLF)
                    ConsoleWrite("- File Name  : " & $iFileName & @CRLF)
                    ConsoleWrite("- Remote Dir : " & $iRemoteDir & @CRLF)
                    ConsoleWrite("- Remote File: " & $iRemoteFile & @CRLF)
                    ConsoleWrite("- Local File : " & $iLocalFile & @CRLF)

;~                  _FTP_DirCreate($iRemotePath & '\' & $iPathName)
;~                  _FTP_ProgressUpload($hFTPSession, $aDirList[$d] & '\' & $iFileName, $iRemotePath & '\' & $iPathName)

                Next
            EndIf
        Next
    EndIf

EndFunc   ;==>_Sequential

Func _IsFile($sPath)
    If Not FileExists($sPath) Then Return SetError(1, 0, -1)
    If StringInStr(FileGetAttrib($sPath), 'D') <> 0 Then
        Return SetError(0, 0, 0) ;IsDir
    Else
        Return SetError(0, 0, 1) ;IsFile
    EndIf
EndFunc   ;==>_IsFile

Func _SplitPath($sFilePath, $sReturnType = 0)
    Local $sDrive, $sDir, $sFileName, $sExtension
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", 1)
    If @error Then ; This error should never happen.
        ReDim $aArray[5]
        $aArray[0] = $sFilePath
    EndIf
    $sDrive = $aArray[1]
    If StringLeft($aArray[2], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
    Else
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
    EndIf
    $sFileName = $aArray[3]
    $sExtension = $aArray[4]
    If $sReturnType = 0 Then Return $aArray
    If $sReturnType = 1 Then Return $sDrive
    If $sReturnType = 2 Then Return $sDir
    If $sReturnType = 3 Then Return $sFileName
    If $sReturnType = 4 Then Return $sExtension
    Local $fName = $sFileName & $sExtension
    If $sReturnType = 5 Then Return $fName
    Local $zName = $sDrive & $sDir
    If $sReturnType = 6 Then Return $zName
    ; By Dao Van Trong - TRONG.LIVE
EndFunc   ;==>_SplitPath

 

Edited by VIP
Thanks Subz

Enjoy my work? Buy me a 🍻 or tip via ❤️ PayPal

Posted

The code was based upon folder names in your first post, which does work, however it appears you want to sort by hierarchy maybe?  So can you try:

Local $aFolderPath, $x = 0
Local $aFolderPaths = _FileListToArrayRec(@ScriptDir, "*", 2, 1, 1, 2)
_ArrayColInsert($aFolderPaths, 1)
For $i = 1 To $aFolderPaths[0][0]
    $aFolderPath = StringSplit($aFolderPaths[$i][0], "\")
    If $aFolderPaths[$i - 1][1] = $aFolderPath[0] & "." & $x Then
        $x += 1
        $aFolderPaths[$i][1] = $aFolderPath[0] & "." & $x
    Else
        $x = 0
        $aFolderPaths[$i][1] = $aFolderPath[0] & "." & $x
    EndIf
Next
_ArraySort($aFolderPaths, 1, 1, 0, 1)
_ArrayDisplay($aFolderPaths)

 

Posted

Just my luck I only tested with 9 subfolders :), you just need to turn the second value into a number, you could also use something like the following (probably could be written better) but works:

Local $aFolderPath, $x = 0
Local $aFolderPaths = _FileListToArrayRec(@ScriptDir, "*", 2, 1, 1, 2)
_ArrayColInsert($aFolderPaths, 1)
For $i = 1 To $aFolderPaths[0][0]
    $aFolderPath = StringSplit($aFolderPaths[$i][0], "\")
    If _ArraySearch($aFolderPaths, Number($aFolderPath[0] & "." & $x), 1, 0, 0, 0, 1, 1) > -1 Then
        $x += 1
        $aFolderPaths[$i][1] = Number($aFolderPath[0] & "." & $x)
    Else
        $x = 0
        While _ArraySearch($aFolderPaths, Number($aFolderPath[0] & "." & $x), 1, 0, 0, 0, 1, 1) > -1
            $x += 1
        WEnd
        $aFolderPaths[$i][1] = Number($aFolderPath[0] & "." & $x)
    EndIf
Next
_ArraySort($aFolderPaths, 1, 1, 0, 1)
_ArrayDisplay($aFolderPaths)

 

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