Directory tree structure in an Object

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hisrRB57
Posts: 62
Joined: 13 Jan 2019, 11:43

Directory tree structure in an Object

Post by hisrRB57 » 29 Jun 2022, 02:08

I have the following code:

Code: Select all

#SingleInstance Force
#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.

Quote := chr(34)			; " karakter
Replace_text := ""","""		; "," string
Filelist := {}
FileSelectFolder, F1P,
iL1 := strlen(F1P)
parentfolder := F1P . "\*.*"
loop, files, %parentfolder%, DFR 		; Collect Files
{
	Splitpath,A_LoopFileFullpath,F1N,F1D
	F1SD := substr(F1D,iL1+2)
	F1M := A_LoopFileTimeModified
	F1C := A_LoopFileTimeCreated
	F1A := A_LoopFileAttrib
	F1S := A_LoopFileSize
	F1I := A_Index
	oDir := StrSplit(F1SD,"\")
	iLenoDir := oDir.Length()
	
	if (instr(F1A,"D")) {
		if (ilenoDir = 0)
			FileList[F1N] := {}
		if (ilenoDir = 1)
			FileList[oDir[1],F1N] := {}
		if (ilenoDir = 2)
			FileList[oDir[1],oDir[2],F1N] := {}
	} else {
		if (ilenoDir = 0)
			FileList[F1N] := F1A
		if (ilenoDir = 1)
			FileList[oDir[1],F1N] := F1A
		if (ilenoDir = 2)
			FileList[oDir[1],oDir[2],F1N] := F1A
	}
}			
MsgBox

ExitApp

It generates an object of a directory tree with the same structure as a directory tree.
However this code goes only 2 level deep, due to the way I coded the dimensions.
I wonder if there is something to avoid this like:

key := "oDir[1],oDir[2],F1N"
Filelist[%key%] := F1A

where %key% would be dynamically replaced by its value

iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Directory tree structure in an Object

Post by iPhilip » 29 Jun 2022, 03:23

@hisrRB57, I think you need to define a recursive function, perhaps something like this:

Code: Select all

#Include JSON.ahk  ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=627
MsgBox % JSON.Dump(Add(A_ScriptDir), , "`t")

Add(Folder) {
   Directory := {}
   Loop, Files, % Folder "\*", FD
      Directory[A_LoopFileName] := InStr(A_LoopFileAttrib, "D") ? Add(A_LoopFilePath) : ""
   return Directory
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

hisrRB57
Posts: 62
Joined: 13 Jan 2019, 11:43

Re: Directory tree structure in an Object

Post by hisrRB57 » 29 Jun 2022, 13:27

Thanks for your reply iPhilip,
The code you suggested is not working, but it pointed me in the right direction:

Code: Select all

#SingleInstance Force
#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.

Filelist := {}
FileSelectFolder, F1P,
Filelist := AddFolder(F1P,Filelist)
MsgBox % Filelist.count()
ExitApp

AddFolder(Folder,Directory) {
	Loop, Files, % Folder "\*", FD
	{
		if (InStr(A_LoopFileAttrib, "D")) {
			Directory[A_LoopFileName] := {}
			Directory[A_LoopFileName] := AddFolder(A_LoopFileFullPath,Directory[A_LoopFileName])
		} else {
			Directory[A_LoopFileName] := ""
		}
	}
	Return Directory
}
			

iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Directory tree structure in an Object

Post by iPhilip » 29 Jun 2022, 14:28

Great! I am glad it helped. :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Post Reply

Return to “Ask for Help (v1)”