Jump to content

List Sub-Directories


RaySS
 Share

Recommended Posts

How to get a list of all the sub-directories in the current directory?

From Help, I find this example which displays all the sub-directories, but it also lists all the files. I want to limit the display to sub-directories only. If I can get this to work, the next step will be to store all sub-directory names in an array then do further processing on the names.

 

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Assign a Local variable the search handle of all files in the current directory.
    Local $hSearch = FileFindFirstFile("*.*")

    ; Check if the search was successful, if not display a message and return False.
    If $hSearch = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
        Return False
    EndIf

    ; Assign a Local variable the empty string which will contain the files names found.
    Local $sFileName = "", $iResult = 0

    While 1
        $sFileName = FileFindNextFile($hSearch)
        ; If there is no more file matching the search.
        If @error Then ExitLoop

        ; Display the file name.
        $iResult = MsgBox(BitOR($MB_SYSTEMMODAL, $MB_OKCANCEL), "", "File: " & $sFileName)
        If $iResult <> $IDOK Then ExitLoop ; If the user clicks on the cancel/close button.
    WEnd

    ; Close the search handle.
    FileClose($hSearch)
EndFunc   ;==>Example

Thank you.

RaySS

64x Win7 Pro SP1

 

Link to comment
Share on other sites

  • Moderators

RaySS,

Use _FileListToArray with the $FLTA_FOLDERS flag to just return the folders - or _FileListToArrayRec with the $FLTAR_FOLDERS flag if you want to get the entire tree.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Hi Melba23,

Thanks for the reply.

I tried the following, but no joy. See code followed by errors:
 

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Local $sFilePth = "E:\Clif\Doublestar"
 $aList = _FileListToArray ( $sFilePth, "", 2)

For $a In $aList
ConsoleWrite ($aList & @CRLF)
Next

"E:\Clif\Doublestar\List_Dir.au3" (8) : ==> Variable must be of type "Object".:
For $a In $aList
For $a In $aList^ ERROR

 I want to put the names of all subdirectories within E:\Clif\Doublestar into array $aList. Why is "E:\Clif\Doublestar" not being treated as a string when I try to store it into $sFilePth?

Looking ahead, my next goal will be to store the date and time of modification and size of each subdirectory into a second and third column in array $aList next to the associated subdirectory name. Please point me to help on capturing modification date-time and size.

Side question: Every time I press Enter in this BB editor, I get double space (an extra blank line). Is there a preference I can set to achieve single space (just one CTLF)?

Thank you for you help.

Ray

Link to comment
Share on other sites

 

 Ray,

To loop through an array. use code like this:

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Local $sFilePth = "E:\Autoit Dev\"
 $aList = _FileListToArray ( $sFilePth, Default, 2)

If IsArray($aList) Then
    For $a = 1 to $aList[0]
    ConsoleWrite ($aList[$a] & @CRLF)
    Next
EndIf

 

Note that you want to use Default for the second parameter, as "" will list nothing.

 

On a side note.. I really need to figure out how to remove quote blocks.

Edited by kaisies
Link to comment
Share on other sites

Hi Kaisies

Thanks for the reply.

Following your example, the Variable must be of type "Object".: error disappeared when I added a backslash to the end of the main directory name "E:\Clif\Doublestar\". And, of course, your For loop works as desired.

A question about arrays:  Apparently it isn't necessary to declare the maximum number of cells in an array. In this instance, the _FileListToArray function allocates as much space as is necessary to accommodate whatever number of subdirectories it finds. Please confirm.

Can you tell me how to capture the date-time and size info of each subdirectory? How to store this info in a second and third column in $aList[]?

Just like you, I would like to know how to delete a quote block. Also, how to turn off double line feed when pressing the Enter key.

Thank you again.

Ray

Link to comment
Share on other sites

I want to show number of files in selected directories. Here's my code:

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Local $sFilePth = "E:\Clif\Doublestar\", $aNo_Of_Files[100], $aFiles[100]
$aList = _FileListToArray($sFilePth, Default, 2)

If IsArray($aList) Then
    For $a = 1 To $aList[0]
        If Not StringInStr($aList[$a], "for l") Then ;Good hits don't contain "for l" in their name.
            ConsoleWrite($aList[$a])
            ConsoleWrite("   good hit" & @CRLF)
            Local $aSize = DirGetSize($aList[$a], 1) ; extended mode
            ConsoleWrite("Files :" & $aSize[1] & @CRLF) ; show number of files in directory
        EndIf
    Next
EndIf

Here's the error generated:

2015_11_09_022752 BU_385AB   good hit
"D:\Personal\Dad\AutoIT_Scripts\List_Dir.au3" (14) : ==> Subscript used on non-accessible variable.:
ConsoleWrite("Files :" & $aSize[1] & @CRLF)
ConsoleWrite("Files :" & $aSize^ ERROR

I copied the failing line directly from Help. Please tell me why the contents of $aSize[1] are inaccessible. How to fix it?

Thank you,

Ray

Link to comment
Share on other sites

  • Moderators

RaySS,

You need to add the root path string to the folder name, otherwise you will not get the correct return from DirGetSize as it will be using an incomplete path - or you can set the $bReturnPath parameter to return the full path directly from the _FileListToArray function.

On a  side note, it always a good idea to check @error if you have a problem like this - in this case it would have been set giving you a good hint.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Hello M23,

When I used a value of 1 for $bReturnPath as you advised, the script ran exactly as desired. When I tried your suggestion for examining @error, the value of @error was blank. Here's the purposely failing code that is meant to display the value of @error:

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $bReturnPath = True
Local $sFilePth = "E:\Clif\Doublestar\", $aNo_Of_Files[100], $aFiles[100]
$aList = _FileListToArray($sFilePth, Default, 2, 0)

If IsArray($aList) Then
    For $a = 1 To $aList[0]
        If Not StringInStr($aList[$a], "for l") Then ;Good hits don't contain "for l" in their name.
            ConsoleWrite($aList[$a])
            ConsoleWrite("   good hit" & @CRLF)
            Local $aSize = DirGetSize($aList[$a], 1) ; extended mode
            ConsoleWrite("error: " & @error & " <==") ; temporary debugging
            ConsoleWrite("Files :" & $aSize[1] & @CRLF) ; show number of files in directory

        EndIf
    Next
EndIf

Here's the console output:

2015_11_09_022752 BU_385AB   good hit
error: 1<=="D:\Personal\Dad\AutoIT_Scripts\List_Dir.au3" (16) : ==> Subscript used on non-accessible variable.:
ConsoleWrite("Files :" & $aSize[1] & @CRLF)
ConsoleWrite("Files :" & $aSize^ ERROR
->06:40:29 AutoIt3.exe ended.rc:1

The script aborted before it got to my temporary debugging statement. How can I capture and display the value of @error?

Thank you again.

Ray

Link to comment
Share on other sites

  • Moderators

RaySS,

You have the value of @error:

2015_11_09_022752 BU_385AB   good hit
error: 1<=="D:\Personal\Dad\AutoIT_Scripts\List_Dir.au3" (16) : ==> Subscript used on non-accessible variable.:
ConsoleWrite("Files :" & $aSize[1] & @CRLF)
ConsoleWrite("Files :" & $aSize^ ERROR
->06:40:29 AutoIt3.exe ended.rc:1

M23 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Hi M23,

Thanks for clarifying the use of @error.

Now my attempt to grab the date&time of modification is failing because I'm not calling the FileGetTime function correctly. Here's the code:

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $bReturnPath = True
Local $sFilePth = "E:\Clif\Doublestar\", $aNo_Of_Files[100], $aFiles[100]
$aList = _FileListToArray($sFilePth, Default, 2, $bReturnPath)

If IsArray($aList) Then
    For $a = 1 To $aList[0]
        If Not StringInStr($aList[$a], "for l") Then ;Good hits don't contain "for l" in their name.
            ConsoleWrite($aList[$a])
            ConsoleWrite("   good hit" & @CRLF)
            Local $aSize = DirGetSize($aList[$a], 1) ; extended mode
            ConsoleWrite("error: " & @error & " <==  ") ; temporary debugging
            ConsoleWrite("Files :" & $aSize[1] & @CRLF) ; show number of files in directory
            Local $aTime = FileGetTime($sFilePth & $aList[$a], $FT_MODIFIED = 0, $FT_STRING = 1)

            ConsoleWrite($sFilePth & $aList[$a] & @crlf) ;$aList[$a] is blank

            ConsoleWrite("  Date-Time: " & $aTime & @CRLF)
        EndIf
    Next
EndIf

$aList[$a] is fine in the DirGetSize statement, and it looks OK in the FileGetTime statement, I don't get any date&time info for the folder. Here's a few lines of console output:

E:\Clif\Doublestar\2015_11_09_022752 BU_385AB   good hit
error: 0 <==  Files :10001
E:\Clif\Doublestar\2015_11_09_022752 BU_385AB
  Date-Time:
E:\Clif\Doublestar\2015_11_09_022752 BU_bogus1   good hit
error: 0 <==  Files :29
E:\Clif\Doublestar\2015_11_09_022752 BU_bogus1
  Date-Time:

 

Thanks for your continuing help.

Ray

PS: How to turn off doublespacing in this BB editor?

 

 

Link to comment
Share on other sites

This line is written wrong

Local $aTime = FileGetTime($sFilePth & $aList[$a], $FT_MODIFIED = 0, $FT_STRING = 1)

It needs to be like this

Local $aTime = FileGetTime($sFilePth & $aList[$a], $FT_MODIFIED, $FT_STRING)

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thanks to BrewManNH, Melba23, and kaisies.

This segment of my first project is working as desired. Now, I'm looking at Help and snippets for info on sorting multi-dimensional arrays. I'm sure I'll encounter more stumbling blocks, but, when I do, I'll open a new topic.

Again, thanks to you all for helping me and for contributing to this very useful site.

RaySS

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...