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......
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?
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)
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.
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.