How to list ALL the FILES in a DRIVE and ITS FOLDERS AS WELL AS ITS SUB FOLDER!?

closed account (3hMz8vqX)
Hi all,
How will you list all ONLY the files in the entire drive?
for example,
I want to print the file path of all files in it as well as the files in the folder and its subdirs?
ONLY THE FILE PATH!!!
Thankyou everyone in advance!!! :)
Check out boost filesystem. Using the Windows API is a pain for this.
it can be solved without programming.
1. open cmd
2. write following line
dir d: /a /s /b > d:/files_path.txt
3. now, d:/files_path.txt include paths of all files.

meaning
d:
search d drive
/a
show all files
/s
search subdirectory, too
/b
bare format
> d:/files_path.txt
save result into d:/files_path.txt
closed account (3hMz8vqX)
Thankyou, is there any way using WINAPI???
Regards,
Aravind.
Hi,

No point, winapi will not only make the code platform specific besides implementing this in winapi is more difficult and requires more code.
To answer your question though you would use FindFirstFile(), FindNextFile() and the WIN32_FIND_DATA struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

void FindAllFiles(std::string FolderName)
{

WIN32_FIND_DATA FileData;

HANDLE FirstFile = FindFirstFile(&FolderName[0], &FileData);

while(FindNextFile(FirstFile, &FileData))
{
      if(isalpha(FileData.cFileName[0])
     {
         if(FileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
         {
              std::cout << FileData.cFileName << std::endl;
         }
         else
         {
            std::string NewPath = FolderName + FileData.cFileName;
            NewPath = NewPath + "\\*.*";

             FindAllFiles(NewPath);
         }
     }
}

}


Or something like that, I didn't try this code but the basic idea is there. Keep in mind that for the ANSI version of FindFirstFile() the length is limited to MAX_PATH, so more about this in the link below.


- FindFirstFile(): http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

- FindNextFile(): http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx

- WIN32_FIND_DATA: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740(v=vs.85).aspx

- File Attribute Constants: http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx
closed account (3hMz8vqX)
Thankyou very, very much!!!:)
13
14
         if(FileData.dwFileAttributes & ~FILE_ATTRIBUTE_DIRECTORY)
         {
@ Duoas: It looks like it should be faster, it would be a few less push\pull instructions. I've always coded for readability and relied on the compilers ability to optimize for speed. Although I honestly don't know the limits to it's abilities in this regard.
I don't think this is about speed, I think this is about meaning. Duoas is suggesting that dwFilAddtributes is a flag variable which, in the presence of more than once flag, will not be equal to any flag (and thus the inequality comparison will be true even for directories).
closed account (3hMz8vqX)
Why cant you use :
 
if(file.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)

...?
because its not working my code does not print anything?
Because Duoas is correct.
@ LB: dwFileAttributes is a DWORD, which is an unsigned int so "!=" should be fine. I use this code (actually the inverse of it) in production and it's never given me any trouble.

@OP: My sample code doesn't have any error checking, I would start by adding that to make sure that FindFirstFile() returns a valid HANDLE.
Beautiful .. i was looking for code like this thanks a lot!
Computergeek01 wrote:
@ LB: dwFileAttributes is a DWORD, which is an unsigned int so "!=" should be fine. I use this code (actually the inverse of it) in production and it's never given me any trouble.
Pretty sure with flag-based variables it doesn't work like that.
Last edited on
@ LB: I figured out what he was saying, and you were close with that example you deleted. Duoas was pointing out that a file\folder can have a combination of attributes and my code doesn't account for that. For example if a folder was hidden then it's dwFileAttribues value would be at least 18 (FILE_ATTRIBUTE_DIRECTORY + FILE_ATTRIBUTE_HIDDEN) and my code would over look it.
Last edited on
L B wrote:
I don't think this is about speed, I think this is about meaning. Duoas is suggesting that dwFilAddtributes is a flag variable which, in the presence of more than once flag, will not be equal to any flag (and thus the inequality comparison will be true even for directories).
:(
@ LB: Yes you were right, I'm not really paying enough attention to any particular thing right now so don't take it personally. I should be focusing on the 13 laptops that are 75 miles away from me and keep going to sleep while I'm trying to update Java on them. But because I'm a terrible employee I'm here instead because dividing my attention like this keeps me calm for some reason.

@ OP: How's that error reporting look? Did you find out what went wrong?
Last edited on
Topic archived. No new replies allowed.