Page 1 of 1

Sort list of file paths in folder\subfolders order

Posted: 25 Dec 2019, 08:42
by GEV
Is there a way to sort such a list

list =
(
c:\folder a\wfbt.txt
c:\folder c\blah.txt
c:\folder b\terp.txt
c:\folder b\subfolder c\phbt.txt
c:\folder a\subfolder a\hfbt.txt
)

in folder\subfolders order (alphabetically in each directory)?

That is, the result should be

c:\folder a\wfbt.txt
c:\folder a\subfolder a\hfbt.txt
c:\folder b\terp.txt
c:\folder b\subfolder c\phbt.txt
c:\folder c\blah.txt

Re: Sort list of file paths in folder\subfolders order

Posted: 25 Dec 2019, 16:00
by Odlanir

Code: Select all

list =
(
c:\folder a\wfbt.txt
c:\folder c\blah.txt
c:\folder b\terp.txt
c:\folder b\subfolder c\phbt.txt
c:\folder a\subfolder a\hfbt.txt
)
Sort, List
MsgBox % list

Re: Sort list of file paths in folder\subfolders order

Posted: 25 Dec 2019, 16:23
by flyingDman
@GEV can you be more explicit? How would a file like C:\zbra.txt rank? based on your result example, it would go to the top as c:\folder a\wfbt.txt ranks above c:\folder a\subfolder a\hfbt.txt and c:\folder b\terp.txt above c:\folder b\subfolder c\phbt.txt
. A simple "sort" as shown by @Odlanir would put C:\zbra.txt at the bottom. Sort is by far the easiest solution. Sorting based on the way you seem to want it is way more complicated...

Re: Sort list of file paths in folder\subfolders order

Posted: 25 Dec 2019, 17:00
by GEV
@Odlanir,
thanks for your answer.

@flyingDman,
I would like to sort the files in the order they appear in the exlorer tree (files in the root folder before those in the subbolders and in alphabetical order in each of them).

Re: Sort list of file paths in folder\subfolders order

Posted: 25 Dec 2019, 17:39
by rommmcek
Loks like you need logical sort:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

list =
(
c:\folder a\wfbt.txt
c:\folder c\blah.txt
c:\folder b\terp.txt
c:\folder b\subfolder c\phbt.txt
c:\folder a\subfolder a\hfbt.txt
)
list2:= list
;Sort, List
;SortNW(s1, s2)
;MsgBox % list


;https://www.autohotkey.com/boards/viewtopic.php?p=246364#p246364
;var := "aBc 3 2 1 11 Abc 12 24 texT 78 94 9 7 72 Letters 99 101 Text", var1 := var
AW := A_IsUnicode ? "W": "A"
Sort, list ;, CL N
Sort, list2, CL F SortN%AW%
MsgBox, %list%`n`n%list2%



;================================================================
SortNA(s1, s2)
;================================================================
{
; for ANSI AHK we must first convert input strings to Unicode, then perform comparison
s1a := StrGet(&s1, "UTF-8"), s2a := StrGet(&s2, "UTF-8")
VarSetCapacity(s1b, 2*StrPut(s1a, "UTF-16"), 0), VarSetCapacity(s2b, 2*StrPut(s2a, "UTF-16"), 0)
StrPut(s1a, &s1b, "UTF-16"), StrPut(s2a, &s2b, "UTF-16")
return DllCall("shlwapi\StrCmpLogicalW", "Ptr", &s1b, "Ptr", &s2b, "Int")
}
;================================================================
SortNW(s1, s2)
;================================================================
{
return DllCall("shlwapi\StrCmpLogicalW", "Str", s1, "Str", s2, "Int")
}

Re: Sort list of file paths in folder\subfolders order  Topic is solved

Posted: 26 Dec 2019, 04:15
by lvalkov
Implemented in v2-a108.
Backporting to v1 would necessitate substituting the Map for another case-sensitive collection, such as a Scripting.Dictionary or a different homebrewed one.

Re: Sort list of file paths in folder\subfolders order

Posted: 29 Dec 2019, 08:11
by GEV
Amazing! THANKS a million!!!