How to get all files in a folder recursively relative to its parent?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

How to get all files in a folder recursively relative to its parent?

Post by fabricio234 » 24 Aug 2023, 23:09

This is what I did but when one folder is inside another it's not saved inside its parent key in the object:

Code: Select all

dir = C:\Users\%A_UserName%\Documents

; Just to reproduce a folder with subfolders/files -------
FileCreateDir, %dir%\A
FileCreateDir, %dir%\A\B
FileCreateDir, %dir%\A\B\B1
FileCreateDir, %dir%\A\C
FileCreateDir, %dir%\A\C\C1

FileAppend,, %dir%\A\a.txt
FileAppend,, %dir%\A\B\b.txt
FileAppend,, %dir%\A\B\B1\b1.txt
FileAppend,, %dir%\A\C\c.txt
FileAppend,, %dir%\A\C\C1\c1.txt
; -----------------------------------------------------------------

fileList := {A: {}}
dir := dir "\A"
iterate(dir, "A")
iterate(dir, folder:="")
{
   global 

   Loop, Files, %dir%\*, DFR  ; R option for recursive search
   {
      if (A_LoopFileAttrib == "D")
      {
         fileList[A_LoopFileName] := []

         iterate(A_LoopFileFullPath,  A_LoopFileName)
         continue
      }
      else
      {
         if (A_LoopFileDir != dir)
            continue
 
         fileList[folder].push(A_LoopFileName)
      }
   }
}
Result:
image.png
image.png (9.83 KiB) Viewed 408 times

I'm trying to store all folders in the object in relation to their parents,
B and C should have been inside A
B1 inside A->B
C1 inside A->C

What i'm trying to achieve is something like this:
image.png
image.png (14.89 KiB) Viewed 382 times
I wonder if there's a better way to store this other than obj/array
at the ending ill convert this info to json

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

Re: How to get all files in a folder recursively relative to its parent?

Post by mikeyww » 25 Aug 2023, 00:09

Although it appears that you want an array of arrays, your script does not push an array at any point, right? If you never create an array of arrays or push any array, then you immediately know that your approach fails. You are building a shallow array at all times.

You might want to walk through a single example of what should happen with a file nested under two directories. Go step by step, to determine what array should be created, and what should be pushed.

It seems that for a given array of arrays, you then want to push a new array for each subdirectory, and push a file name for each file. To accomplish this, you might need to parse the file's path. Simply creating a recursive function does not handle that part in itself.

Explained: Arrays of arrays
Last edited by mikeyww on 25 Aug 2023, 00:17, edited 2 times in total.

fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

Re: How to get all files in a folder recursively relative to its parent?

Post by fabricio234 » 25 Aug 2023, 00:15

What do you mean by parse the file path? also i didn't get it, with an array of arrays how i will know what is a folder and what is a file?

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

Re: How to get all files in a folder recursively relative to its parent?

Post by mikeyww » 25 Aug 2023, 00:18

Walk through an example as I mentioned. If the path is C:\A\B\B1\b1.txt, what array should be populated? If you want to populate an array of arrays, then where will such an array be created? After you walk through an example, you will have a better idea of what is missing from your script.

You already know how to distinguish a directory from a file. Your script does that, right? Although your approach to checking the attribute might work, I recommend using :arrow: InStr as shown in the documentation.

fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

Re: How to get all files in a folder recursively relative to its parent?

Post by fabricio234 » 25 Aug 2023, 00:51

what i didn't understand is how to populate the arrays according to the path, its confuse, It also fails when it leaves from a subfolder to a different parent

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

Re: How to get all files in a folder recursively relative to its parent?

Post by mikeyww » 25 Aug 2023, 00:59

Instead of thinking about every situation, think about one situation. I will ask a third time: if the path is C:\A\B\B1\b1.txt, what array should be populated?

Post Reply

Return to “Ask for Help (v1)”