#### Archived

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

# How to get all files in directory?

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

## Recommended Posts

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

##### 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    );	 ParametershDlgIdentifies the dialog box that contains the combo box. lpPathSpecPoints to a null-terminated string of the following form: [drive:][\]][directory\[...]][filename] 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. nIDComboBoxSpecifies 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. nIDStaticPathSpecifies 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. uFiletypeSpecifies the attributes of the files to be displayed. It can be any combination of the following values: Value	MeaningDDL_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 ValuesIf 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. RemarksIf 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 on other sites
Check out this nifty set of functions.

##### 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);Files.push_back(w32Find.cFileName);while (FindNextFile(findHandle, &w32Find)) {    Files.push_back(w32Find.cFileName);}FindClose(findHandle);

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

##### Share on other sites
that''s ok, i''ve found a solution...uses the system() call and then calls dir /b

##### 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 on other sites
You can always check out how boost does it.

##### Share on other sites
quote:
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 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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633324
• Total Posts
3011366
• ### Who's Online (See full list)

There are no registered users currently online

×