I would recommend people to have a go at both problems, the sorting and the tabulation.
I sort of did the tabulation by intuition rather than thinking, although from tests it appears correct. It was one simple principle, but with one aspect of complication that I used the vIsMatch variable for.
In comments for the functions, I talk about two different sort orders, if there are any standard names for these I would be glad to know. I was tempted to ask on Stack Overflow ... however the rules about acceptable questions there are unclear (and it's easy to get a six-month question ban), so any forum recommendations for forums where you can actually ask sensible reasonable questions about IT, and grow as a programmer, are welcome.
Code: Select all
q:: ;list files in a special tabulated format
vText := " ;continuation section
(
\a\b\c
\a\b\c\1\2
\a\b\c\1\3
\a\b\c\2\2
\a\b\c\2\3
\a\b\d
\a\b\d\1\2
\a\b\d\1\3
\a\b\d\2\2
\a\b\d\2\3
\a\c\d\1\2
\a\c\d\1\3
\a\c\d\2\2
\a\c\d\2\3
\d\e\f
)"
;replace 'if 0' with 'if 1', to use some real paths:
if 0
{
vText := ""
VarSetCapacity(vText, 1000000*2)
vDir1 = %A_Desktop%\New Folder
vPos := StrLen(vDir1) + 2
Loop, Files, % vDir1 "\*", FR
vText .= SubStr(A_LoopFileFullPath, vPos) "`n"
vText := SubStr(vText, 1, -1)
}
;Sort, vText, F JEE_SortPathFindNextFile
Sort, vText, F JEE_SortPathRegEdit
MsgBox, % vText
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*4*2)
oPath2 := {}
Loop, Parse, vText, `n
{
oPath := StrSplit(A_LoopField, "\")
vIsMatch := 1
Loop, % oPath.Length()
{
if !vIsMatch || !(oPath[A_Index] = oPath2[A_Index])
vIsMatch := 0, vOutput .= JEE_StrRept("`t", A_Index-1) oPath[A_Index] "`r`n"
}
oPath2 := oPath.Clone()
}
Clipboard := vOutput
oPath := oPath2 := ""
MsgBox, % vOutput
return
;==================================================
JEE_StrRept(vText, vNum)
{
return StrReplace(Format("{:" vNum "}","")," ",vText)
;return StrReplace(Format("{:0" vNum "}",0),0,vText)
}
;==================================================
;FindNextFile style, 'finish the current folder':
;a
;b
;a\a
;a\b
;a\a\a
;a\a\b
;a\b\a
;a\b\b
;b\a
;b\b
;b\a\a
;b\a\b
;b\b\a
;b\b\b
JEE_SortPathFindNextFile(vTextA, vTextB, vOffset) ;for use with AHK's Sort command
{
StringUpper, vTextA, vTextA
StringUpper, vTextB, vTextB
if ("" vTextA = "" vTextB)
return -vOffset
oTempA := StrSplit(vTextA, "\")
oTempB := StrSplit(vTextB, "\")
vMin := oTempA.Length() < oTempB.Length() ? oTempA.Length() : oTempB.Length()
Loop, % vMin
if !(oTempA[A_Index] = oTempB[A_Index])
{
vTextA := oTempA[A_Index], vTextB := oTempB[A_Index], vIndex := A_Index
break
}
if (vIndex = vMin) && !(oTempA.Length() = oTempB.Length())
if (oTempA.Length() = vMin)
return -1
else if (oTempB.Length() = vMin)
return 1
vSCS := A_StringCaseSense
StringCaseSense, On
vRet := ("" vTextA) > ("" vTextB) ? 1 : ("" vTextA) < ("" vTextB) ? -1 : -vOffset
StringCaseSense, % vSCS
return vRet
}
;==================================================
;RegEdit style, 'move on as soon as possible':
;a
;a\a
;a\a\a
;a\a\b
;a\b
;a\b\a
;a\b\b
;b
;b\a
;b\a\a
;b\a\b
;b\b
;b\b\a
;b\b\b
JEE_SortPathRegEdit(vTextA, vTextB, vOffset) ;for use with AHK's Sort command
{
StringUpper, vTextA, vTextA
StringUpper, vTextB, vTextB
if ("" vTextA = "" vTextB)
return -vOffset
oTempA := StrSplit(vTextA, "\")
oTempB := StrSplit(vTextB, "\")
vMin := oTempA.Length() < oTempB.Length() ? oTempA.Length() : oTempB.Length()
Loop, % vMin
if !(oTempA[A_Index] = oTempB[A_Index])
{
vTextA := oTempA[A_Index], vTextB := oTempB[A_Index], vIndex := A_Index
break
}
if (vIndex = vMin) && (vTextA = vTextB)
if (oTempA.Length() = vMin)
return -1
else if (oTempB.Length() = vMin)
return 1
vSCS := A_StringCaseSense
StringCaseSense, On
vRet := ("" vTextA) > ("" vTextB) ? 1 : ("" vTextA) < ("" vTextB) ? -1 : -vOffset
StringCaseSense, % vSCS
return vRet
}
;==================================================