Creating a File Tree from a String

Get help with using AutoHotkey and its commands and hotkeys
User avatar
hasantr
Posts: 668
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Creating a File Tree from a String

Post by hasantr » 30 Oct 2020, 07:21

With a little effort, I created a tree in ways. For TreeView. But I don't think it's nice. Do you have any better coding suggestions? I want to see what could happen. Simpler and shorter. :P 8-)

Code: Select all

Paths :=
(
"C:\abc.d
C:\mbc.d
C:\dbc.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
"
)

Paths := StrSplit(Paths,"`n")
T := 0
TO := {}
For, K,V in Paths
{	
	SplitPath,V,FName,Dir
	DirUpDir := (SubStr(Dir,StrLen(Dir),1) = "\" ? SubStr(Dir,1,StrLen(Dir) -1) : Dir) ;Dizinin bir alt dizinini bulur, Sonda fazladan / karakteri olup olmamasını dikkate alır
	SplitPath,DirUpDir,,DirUpDir
	if(Dir == "")
		Continue
	Dir 	:= Dir . "\"
	DirUpDir 	:= DirUpDir . "\"
	if(TO.HasKey(DirUpDir) == false){
		TO[Dir] := 1
		NewDir := 1
	}
	if(TO.HasKey(Dir) == false){
		TO[Dir] := TO[DirUpDir] + 1
		NewDir := 1
		Firsts := 1
	}
	if(Dir == DirBack || Firsts := 1 || A_Index == 1)
	{
		if(NewDir == 1)
		{
			Loop, % TO[Dir]
				Stack .= "`t"
			Stack .= Dir "`n"
			NewDir := 0
		}
		Loop, % TO[Dir] + 1
			Stack .= "`t"
		Stack .= Dir . FName  "`n"
		Firsts := 0
	}
}
MsgBox % Stack

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

Re: Creating a File Tree from a String

Post by TheDewd » 30 Oct 2020, 13:06

I couldn't figure out how to do it, but I'm very curious as well!

Here's one way to do it (Warning! It's stupid! haha):

Code: Select all

#SingleInstance, Force

Paths =
(
C:\abc.d
C:\mbc.d
C:\dbc.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
)

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

TempPath := A_Desktop "\TEMP_Tree"

; Create Folders
For Index, Value In Paths {
	SplitPath, Value, Name, Dir, Ext, NameNoExt, Drv

	FolderName := StrReplace(Dir, Drv, TempPath)

	If (InStr(FileExist(FolderName), "D") = 0) {
		FileCreateDir, % FolderName
	}
}

; Create Files
For Index, Value In Paths {
	SplitPath, Value, Name, Dir, Ext, NameNoExt, Drv

	NewPath := StrReplace(Value, Drv, TempPath)

	If (!FileExist(NewPath)) {
		FileAppend,, % NewPath
	}
}

ClipboardBak := Clipboard

RunWait, % ComSpec " /c tree /A /F """ TempPath """ | Clip",, Hide

;FileDelete, % TempPath "\*.*"

TreeContents := RegExReplace(Clipboard, "^\s+|\s+$")

Clipboard := ClipboardBak

MsgBox, % SubStr(TreeContents, InStr(TreeContents, "|"))

Output:

Code: Select all

|   abc.d
|   dbc.d
|   mbc.d
|
+---Fil1
|   |   Dele.e
|   |   Delema.d
|   |
|   \---File2
|           Alada.d
|           Deledmad.d
|
\---Fil3
        mada.d
        madaf.d
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.

Post Reply

Return to “Ask For Help”