Sign in to follow this  
Followers 0
JimC

Directory tree to array

15 posts in this topic

I cant find a function to list a directory structure and assign each subdir to an array. does anyone have an idea on how to accomplish this ?

Meaning in you have a root directory d and many subdirectories like

d/1/a

d/1/a/1

d/1/b

I would like to assign each level to an array. Any help you provide would be greatly appreciated.

Share this post


Link to post
Share on other sites



I cant find a function to list a directory structure and assign each subdir to an array. does anyone have an idea on how to accomplish this ?

Meaning in you have a root directory d and many subdirectories like

d/1/a

d/1/a/1

d/1/b

I would like to assign each level to an array. Any help you provide would be greatly appreciated.

See _FileListToArray() Use the directories only paramater.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I cant find a function to list a directory structure and assign each subdir to an array. does anyone have an idea on how to accomplish this ?

Meaning in you have a root directory d and many subdirectories like

d/1/a

d/1/a/1

d/1/b

I would like to assign each level to an array. Any help you provide would be greatly appreciated.

Look out for _FileListToArrayFaster1e.au3

It did exactly what you are asking for, for a project to re-sort some 10000 files to a new folder structure.

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Look out for _FileListToArrayFaster1e.au3

Would someone happen to know the whereabouts of this script? My searches didn't find any reference.

Like the OP, I'm trying to produce a list of directories AND their subdirectories to a text file. Two levels is enough, but an option to go 3-deep would be even better.

Thanks for any help.

Share this post


Link to post
Share on other sites

qwert,

Look at the RecFileListToArray UDF in my sig - you can go down as deep as you want. :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

That's a very impressive set of capabilities. I was able to quickly get the result I wanted by using ... , "*", 2, -1, 1, 1)

Would you happen to be able to suggest a modification to add the directory sizes as a column in the results array?

Or should I just loop through the result array and use DirGetSize to build a separate array?

Thanks for your help.

Edited by qwert

Share this post


Link to post
Share on other sites

qwert,

That's a very impressive set of capabilities

Thanks. :D

As I have no real use for it myself, it would take more modification to the UDF than I am prepared to undertake to add the sizes. I would just loop through the array entries using DirGetSize to build a separate array. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

M23, I can certainly understand your point about not diving into the UDF for new features ... but can you help me with one point:

Is it possible for me to extend the returned array with an additional column for the DirGetSize results that I obtain in my loop? I've read some of your posts regarding arrays and it appears that ReDim won't work. But is there a way to define $aArray ahead of time so that it has room for my results? As you tell, I'm not well versed in using arrays.

Thanks again.

Share this post


Link to post
Share on other sites

What you are looking for is called a 2D array (Imagine it like a table, you mentioning column is a great example). It would be possible, so long as the created array was already 2D.

I am not 100% sure how the UDF works (I havent seen it before) but if you like, I can take a look into the directory size for you.

Share this post


Link to post
Share on other sites

Mikeman, I greatly appreciate your offer to take a look. My UDF call is $aArray = _RecFileListToArray("C:", "*", 2, -1, 1, 1)

What I don't understand is where the array is really defined ... and where it could be changed to be 2D. Would it be in my script? ... or in the UDF? If I can get one of these under my belt, I think using arrays will start to make more sense to me.

The only use of arrays I've made so far is what I would call a static array (e.g., $nArray [2] [3]). The idea of a "dynamic" array whose size is a result of the processing isn't intuitive ... at least not to me.

Share this post


Link to post
Share on other sites

qwert,

I suggest you create a suitably sized 2D array once you have the names and then transfer the names and add the sizes to it like this: ;)

#include <Array.au3>
#include <RecFileListToArray.au3>

; Where we start
$sRoot = "M:"

; Look in the folder - match all - folders only - go down just 1 level - sort the results - path relative to root
$aList = _RecFileListToArray($sRoot, "*", 2, -1, 1); , 1) Already the default setting

; Create a suitably sized 2D array to hold the names and sizes - count in the [0][0] element
Global $aName_Size[$aList[0] + 1][2] = [[$aList[0]]]

; Now loop through the original array
For $i = 1 To $aList[0]
    ; Transfer the names to the [0] element
    $aName_Size[$i][0] = $aList[$i]
    ; Get sizes to put in the [1] element
    $aName_Size[$i][1] = DirGetSize($sRoot & "" & $aList[$i], 2)
Next

; Remove the original aray to save memory
Global $aList = 0

; Display the result
_ArrayDisplay($aName_Size)

Please ask if you have any questions. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Global $aName_Size[$aList[0] + 1][2] = [[$aList[0]]]

Thanks, M23. That is very straightforward ... except for the second half of the array declaration. The first half creates the needed 2D array of a size one greater than the count returned by the operation. But the "equals" part ... is it loading the array with the names?

Share this post


Link to post
Share on other sites

qwert,

The second part of the line is preloading the array as it is created. You can do this as follows:

Global $aArray[2] = ["Element [0]", "Element [1]"]

Because this is a 2D array the syntax gets a bit more complicated:

Global $aArray[2][2] = [["Element [0][0]", "Element [0][1]"], ["Element [1][0]", "Element [1][1]"]]

As all I wanted to do was to put the count into Element [0][0] I only filled in that bit of the syntax.

The names are transferred into the new array in the loop. ;)

All clear? Please ask if not. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yes, I think I've got it ... but only when I realized that the "equals" part is only loading the first element of the results (the count) and is not interative. IOW, the equals sign in the syntax threw me because it doesn't mean equate in this instance ... it means "initialize the array with the following series of values" ... and in this script example, there is only one value in the series.

Thanks very much for your help.

Share this post


Link to post
Share on other sites

qwert,

My pleasure. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0