Page 1 of 2

GetLogicalFolders?

Posted: Sat Mar 29, 2008 9:38 am
by WedNESday
Obviously there is no such Windows API function, but I already use the get logical drives functions to get drive info, however I don't know how to get a folder or file list. Which function can I use to get a folder and a file list? I would use OPENFILENAME and GetOpenFileName, but I am creating my own custom box.

Posted: Sat Mar 29, 2008 12:13 pm
by tepples
Look for "dirent" by Kevlin Henney. It implements a subset of the POSIX opendir(), readdir(), and closedir() functionality in Windows. I used it in a ROM builder for SMSAdvance.

Which graphics API are you using? Allegro? SDL? Straight DirectX?

Posted: Sat Mar 29, 2008 12:35 pm
by WedNESday
No, that doesn't seem to be what I'm looking for. I'm using the Windows API, but I don't mind using C++.

Posted: Sat Mar 29, 2008 12:40 pm
by dXtr

Posted: Sat Mar 29, 2008 1:18 pm
by tepples
WedNESday wrote:No, that doesn't seem to be what I'm looking for.
Then could you please explain what you are looking for?

Posted: Sat Mar 29, 2008 4:09 pm
by WedNESday
Ok, I have a list of drives, and I can get a list of *.whatever files in a specific directory using FindxFile. What I need to be able to do is get a list of directories in a drive.

Posted: Sun Mar 30, 2008 5:07 am
by tepples
Do you want a list of folders/directories in the current folder, or a list of all folders at all levels of a drive, recursively?

Posted: Sun Mar 30, 2008 6:41 am
by WedNESday
I need a list of all folders in a drive and then in the folders themselves. So both.

Posted: Sun Mar 30, 2008 8:01 am
by tepples
What you want to do is walk the directory tree, making a list. The algorithm is roughly as follows:

Code: Select all

To walk a tree:
  Clear the list of folders and the list of files
  Walk the folder at the root of the tree

To walk a folder:
  For each file or folder inside the folder:
    If the found object is a folder:
      Add the found object to the list of folders
      Walk the found object
    Else:
      If the found object is a file of a desirable type:
        Add the found object to the list of files
What FindxFile lets you do is "For each file or folder inside the folder". "Clear" and "add" are methods of your programming environment's list container, whether it be an array list or a linked list, or whether it be whether it be a C++ STL list, a list in the standard library of any other language, or a custom list. The rest is recursion.

Posted: Mon Mar 31, 2008 5:41 am
by WedNESday
I've done it now. FindFirstFile("*."... for the folders and FindFirstFile("*.nes"... for the files. Thanks ;).

Posted: Mon Mar 31, 2008 6:13 am
by tepples
WedNESday wrote:I've done it now. FindFirstFile("*."... for the folders
I wouldn't recommend trying to distinguish folders from files purely based on their names. In FAT and NTFS, folders are allowed to have a dot in their names, and folders are even allowed to have names ending in .nes.

EDIT: I looked up FindFirstFile(), and it seems that family of functions puts the result in a struct called WIN32_FIND_DATA, which appears to be Win32's analog of POSIX's struct stat. For each entry that is a folder, (st->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) should be nonzero.

Posted: Mon Mar 31, 2008 6:22 am
by Marty
WIN32_FIND_DATA::dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY can be used to check whether it's a directory or not.

Posted: Sun Apr 27, 2008 9:44 am
by WedNESday
Can FindFirstFile/FindNextFile have multiple file filters? (*.bin; *.nes etc.)

Posted: Sun Apr 27, 2008 10:34 am
by Dwedit
I just ended up using Boost

Posted: Sun Apr 27, 2008 1:50 pm
by WedNESday
Dwedit wrote:I just ended up using Boost
What?