Jump to content

Listing directories one level


Recommended Posts

Hi, I'm trying to list the directories under "C:\Users" and I came up with the code below:

#Include <File.au3>
#Include <Array.au3>

$folderList=_FileListToArray("C:\Users", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf
_ArrayDisplay($folderList,"List")

Now the problem is that the code above will list hidden folders "Default" and "Default User" too which I don't want. Is there a way to list only folders that are not hidden?

Link to comment
Share on other sites

You can make your own listing function using FileFindFirstFile and FileFindNextFile. YOu can distinguish between hidden and non-hidden folders with the use of FileGetAttrib.

Or you can try the search feature of the forums. There are some file listing algorithms posted here that can be used or modified to suit your purpose.

Link to comment
Share on other sites

Ok so I used FileGetAttrib but I don't know why it doesn't get the attribs. I don't know what I'm doing wrong.

#Include <File.au3>
#Include <Array.au3>

$folderList=_FileListToArray("C:\Users", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf
For $i = 1 To $folderList[0]
    $attrib = FileGetAttrib($folderList[$i])
    If @error Then
        MsgBox(4096,"Error", "Could not obtain attributes.")
    Else
        If StringInStr($attrib, "H") Then
            _ArrayDelete($folderList, $i)
        EndIf
    EndIf
Next

_ArrayDisplay($folderList,"List")
Link to comment
Share on other sites

Using _ArrayDelete in this way is wrong. _ArrayDelete removes the element at the specified index and resizes the array after deletion. Combining that with a For...Next loop will make the loop fail. What happens when an element is deleted is that after the removal, the next element takes the index of the element that was just removed. This makes you skip the processing of the next element after a deletion, not to mention your array is one element shorter after a deletion which will result to an out of bounds array access when a deletion is performed.

Link to comment
Share on other sites

You might want to consider excluding System directories as well...

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

$path = "C:\temp\"
$folderList = _FileListToArray($path, "*", 2)
If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf

For $i = 1 To $folderList[0]
    If $i > $folderList[0] Then ExitLoop
    $attrib = FileGetAttrib($path & $folderList[$i])
    If @error Then
        MsgBox(4096, "Error", "Could not obtain attributes.")
    Else
        If StringInStr($attrib, "H") Or StringInStr($attrib, "S") Then
            _ArrayDelete($folderList, $i)
            $i -= 1
            $folderList[0] -= 1
            ;_ArrayDisplay($folderList,"List")
        EndIf
    EndIf
Next

_ArrayDisplay($folderList,"List")
Link to comment
Share on other sites

Here is a sample for people who might have the same problem I had. This will list all users (depending on the OS) and check if they have hidden or system attrib. If they have, it will remove it from the array. And finally will make a GUI that will list the users in checkbox form.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <File.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$usersDis = 24
If @OSVersion = "WIN_XP" Then
    $path = "C:\Documents and Settings\"
Else
    $path = "C:\Users\"
EndIf

$folderList = _FileListToArray($path, "*", 2)
If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf

For $i = 1 To $folderList[0]
    If $i > $folderList[0] Then ExitLoop
    $attrib = FileGetAttrib($path & $folderList[$i])
    If @error Then
        MsgBox(4096, "Error", "Could not obtain attributes.")
    Else
        If StringInStr($attrib, "H") Or StringInStr($attrib, "S") Then
            _ArrayDelete($folderList, $i)
            $i -= 1
            $folderList[0] -= 1
        EndIf
    EndIf
Next

$usersFrm = GUICreate("Users", 193, 135, 192, 124)
$usersLbl = GUICtrlCreateLabel("Users:", 8, 8, 40, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
For $i = 1 To $folderList[0]
    $folderList[$i] = GUICtrlCreateCheckbox($folderList[$i], 8, $usersDis, 97, 17)
    $usersDis += 16
Next

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
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...