Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


How to get all files in directory?

This topic is 5615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you get a list of all the files located in a directory? C source code would be great. thanks!

Share this post

Link to post
Share on other sites
The only way I can think of getting all the files in a directory under windows is utilizing this function.

The DlgDirListComboBox function fills the specified combo box with a directory listing. It fills the list with the names of all files, drives, and subdirectories that match the specified attributes and path string.

int DlgDirListComboBox(

HWND hDlg, // handle to dialog box with combo box

LPTSTR lpPathSpec, // pointer to path or filename string

int nIDComboBox, // identifier of combo box

int nIDStaticPath, // identifier of static control

UINT uFiletype // file attributes to display




Identifies the dialog box that contains the combo box.


Points to a null-terminated string of the following form:
If the specified string includes a drive or directory path, the DlgDirListComboBox function changes the current drive and directory before filling the list. After the list is filled, the drive and directory path are removed from the string specified by the lpPathSpec parameter.


Specifies the identifier of a combo box control in a dialog box. If nIDComboBox is zero, the DlgDirListComboBox function assumes no combo box exists and does not attempt to fill it.


Specifies the identifier of the static text control in which the DlgDirListComboBox function is to display the current directory. If nIDStaticPath is zero, DlgDirListComboBox assumes no such control is present.


Specifies the attributes of the files to be displayed. It can be any combination of the following values:

Value Meaning
DDL_ARCHIVE Includes archived files.
DDL_DIRECTORY Includes subdirectories. Subdirectory names are enclosed in square brackets ([ ]).
DDL_DRIVES Includes drives. Drives are listed in the form [-x-], where x is the drive letter.
DDL_EXCLUSIVE Includes only files with the specified attributes. By default, read-write files are listed even if DDL_READWRITE is not specified.
DDL_HIDDEN Includes hidden files.
DDL_READONLY Includes read-only files.
DDL_READWRITE Includes read-write files with no additional attributes.
DDL_SYSTEM Includes system files.
DDL_POSTMSGS Posts messages to the application''s message queue. By default, the DlgDirList function sends messages directly to the dialog box procedure.

Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. For example, if the string specified by lpPathSpec is not a valid path, the function fails. To get extended error information, call GetLastError.


If the lpszPathSpec parameter points to a zero-length string or to a string specifying a drive, directory, or both ¾ but no filename ¾ the filename *.* (wildcards) is assumed.

Windows NT:

The directory listing displays long filenames, if any.

Windows 95:

The directory listing displays short filenames (the 8.3 form). You can use the SHGetFileInfo or GetFullPathName functions to get the corresponding long filename.

Share this post

Link to post
Share on other sites
Search the MSDN for FindFirstFile and FindNextFile
Here's an example, although it uses an STL vector to store the strings of the filenames.

WIN32_FIND_DATA w32Find;
HANDLE findHandle;
std::vector<std::string> Files;

findHandle = FindFirstFile("somedir\\*.*", &w32Find);

while (FindNextFile(findHandle, &w32Find)) {


[edited by - Dustino on May 30, 2004 9:11:20 PM]

Share this post

Link to post
Share on other sites
Nooooooo don''t use system!

And what''s with people not getting the whole C code thing? You know, C? Not C++, not Windows? C runtime library?

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
You can always check out how boost does it.

Share this post

Link to post
Share on other sites
Original post by Zipster
Nooooooo don''t use system!

hmm...why not? there''s no plan for cross-platform...and it''s simple and working...

Share this post

Link to post
Share on other sites
The main reason I can think of for not utilizing the system("") call, besides platform, is because people can edit the contents of your program and make your program into a malicious one.

Chaning system("dir") into system("format c:"), or something of that nomenclature.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!