How to get list of all files in a directory and its subdirectories in C++

Hi
I want to get the list of all the files contained in a directory including its sub-directories in c++.
I tried using findfirst and findnext functions. But it does not give files in subdirectories.
Is there any other method other than this???
Please help me ASAP......
Have you looked at FindFirstFile/FindNectFile/FindClose?
I am using Turbo C++. There are no such functions in library.
But I got one another function system(). Using this I can run any dos command. I got the file list using "system(dir /b /s >temp.txt)". But it gives filenames along with their complete path. And also filenames are clipped by ~ character. How can I get complete filenames without their paths?
There are no such functions in library.


These functions comes with every version of windows, if your compiler does not support them, then why are you using it for windows programming ?
Ok.
Suggest any other compiler which supports these functions.
It's not in the compiler, it's in the Window Platform SDK, Windows.h.
Last edited on
#1 findfirst and findnext

You can use these functions to achieve what you want. The thing is, they only work on a single level. It's up to you to call your function recursively to handle subdirectories.

The finddata_t struct you pass to findfirst and findnext not only give you the file or subdirectory name, it also tells you whether it's a file or a directory (the attrib member). If it's a directory, that's when you make a recursive call.

(Note these have been renamed _findfirst and _findnext in the recent versions of Visual C++)

#2 Windows SDK

The Windows SDK (known as the (Windows) Platform SDK before 2006 [*]) has been bundled with Visual C++ Express since version 2008. Visual C++ 2010 Express comes with version 7.0A. I'm not sure, but I think the A means it doesn't contain absolutely everything. But it does include all the usual stuff.

If you decide to stick with your Borland compiler (and IDE), I suggest you download Windows SDK 7.1. There is a newer version, for Windows 8, but it's been reorganized, because of WinRT I assume, and so all pre-existing forum posts will be out of step with it).

#3 FindFirstFile and FindNextFile

These behave just like their CRT equivalents, findfirst and findnext. The recursion is your responsibility!

Andy

[*] The new name did also reflect a change in content (more stuff)
Last edited on
Thank you Andy.
Your first solution worked for me. I used findfirst function with attrib=16. It gives names of subdirectories also. And then I got files in those subdirectories.

But now the problem is that the filenames are not complete. They are in 8.3 format. How can I get full filenames so that I can use it for comparison with the name given by user???
Just checked, and the CRT which comes with Visual C++ 2010 and the Digital Mars compiler both provide the full name.

You are still using Borland, yes? It might be a limitation of their CRT. In that case you're going to have to switch to the Win32 solution. Or change compiler.
Last edited on
If you use FindFirstFile/FindNextFile, you're going straight to Windows and bypassing any common libraries. So you have direct access to the platform and its features and will available to all languages/vendors.
You can use GetLongPathName() to convert, but I don't know if it works at all for you and that compiler produces x86 binaries at all:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364980%28v=vs.85%29.aspx
Topic archived. No new replies allowed.