File Tree from Text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

File Tree from Text

Post by TheDewd » 15 Nov 2020, 12:24

I was inspired by the following post (https://www.autohotkey.com/boards/viewtopic.php?f=76&t=82679) to try making a script to convert text file paths to a file tree.

I think I'm very close to having a working solution, however I'm having difficulty with parent/child directory recursing.

The example posted below works, as long as each directory has only one level of separation from the previous value, but if the path value contains directory levels that weren't previously added, only the last folder is displayed (see "C:\Fil9\Fil8\Fil7\SZZZ1\OneMore\NewFile.azz").

I think I need help with being able to make a folder a child too, instead of always a parent, to list all the directory levels. Not really sure though... I'm actually stuck.

Hopefully I've clearly described the issue. Thanks in advance!

Note: The "visual" of the file tree still needs some work, such as the root, which I will fix after figuring out this issue.

Code: Select all

#SingleInstance, Force

Gui, Font, s10, Consolas
Gui, Margin, 10, 10
Gui, Add, Edit, w340 r18 vEDT
Gui, Show, AutoSize, Example

Paths =
(
C:\abc.d
C:\mbc.d
C:\Fil1\Dele.e
C:\Fil1\Delema.d
C:\Fil1\File2\Deledmad.d
C:\Fil1\File2\Alada.d
C:\Fil3\mada.d
C:\Fil3\madaf.d
C:\Fil9\Fil8\Fil7\SZZZ1\OneMore\NewFile.azz
C:\dbc.d
)

Paths := StrSplit(Paths, "`n")

Parent := {}
Child := {}

For Index, Value In Paths {
	SplitPath, Value, Name, Dir, Ext, NameNoExt, Drv
	Dir := Dir "\"
	RegExMatch(Value, "(.+\\\K|^)[^\\]+(?=\\)", Folder)
	StrReplace(Dir, "\",, Level)
	Parent[Dir] := {"Parent": Dir, "Level": Level, "Folder": Folder}
	Child[Value] := {"Parent": Dir, "Child": Name}
}

CurrParent := ""
PrevParent := ""
Output := ""

For Index1, Value1 In Parent {
	For Index2, Value2 In Child {
		If (Value1.Parent = Value2.Parent) {
			CurrParent := Value2.Parent
				
			If (CurrParent <> PrevParent) {
				PrefixA := Format("{:" Value1.Level * 4 "}", "|-- ")
				Output .= PrefixA . Value1.Folder "`n"
			}

			Prefix := Format("{:" Value1.Level * 4 "}", "|-- ")
			Output .= Prefix . Value2.Child "`n"

			PrevParent := Value2.Parent
		}
	}
}

GuiControl,, EDT, % Output

This is what I get:

Code: Select all

C:
|-- abc.d
|-- dbc.d
|-- mbc.d
    |-- Fil1
    |-- Dele.e
    |-- Delema.d
        |-- File2
        |-- Alada.d
        |-- Deledmad.d
    |-- Fil3
    |-- mada.d
    |-- madaf.d
                    |-- OneMore
                    |-- NewFile.azz

This is what I want:

Code: Select all

C:
|-- abc.d
|-- dbc.d
|-- mbc.d
    |-- Fil1
    |-- Dele.e
    |-- Delema.d
        |-- File2
        |-- Alada.d
        |-- Deledmad.d
    |-- Fil3
    |-- mada.d
    |-- madaf.d
    |-- Fil9
        |-- Fil8
		    |-- Fil7
			    |-- SZZZ1
				    |-- OneMore
					    |-- NewFile.azz

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: File Tree from Text

Post by mikeyww » 15 Nov 2020, 18:13

The output tree is atypical. The inconsistency seems to be with NewFile.azz. Whereas "C:\Fil1\Dele.e" shows "Fil1" and "Dele.e" as siblings (which is also difficult to understand), "C:\Fil9\Fil8\Fil7\SZZZ1\OneMore\NewFile.azz" shows "NewFile.azz" as a child of "OneMore". Thus, identifying the logic in the parsing is rather difficult (and I have not succeeded in doing it).

User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: File Tree from Text

Post by TheDewd » 15 Nov 2020, 19:02

@mikeyww,

It is rather difficult, which I suppose is one reason it's not been done successfully yet with AutoHotkey. Thanks for trying -- I spent a very long time trying to figure it out with no success.

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: File Tree from Text

Post by mikeyww » 15 Nov 2020, 19:57

I mean to say that, aside from AHK itself, I'm not certain what rule you even intend to apply in parsing your tree information, because your intended output shows inconsistencies.

User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: File Tree from Text

Post by TheDewd » 15 Nov 2020, 20:18

mikeyww wrote:
15 Nov 2020, 19:57
I mean to say that, aside from AHK itself, I'm not certain what rule you even intend to apply in parsing your tree information, because your intended output shows inconsistencies.
I don't know either, haha. I think I was just working on it too much and have confused myself. I guess it's fine as-is.

Post Reply

Return to “Ask for Help (v1)”