How can I obtain all of the file names in a directory?

Ben

I am writing a program in C++. I am trying to obtain all of the files in the folder where the program executable is located and store them in a vector. I have been told that the following code should work, but the FindFirstFile operation only finds one file (the name of the folder that it should be searching through). How can I change the code so that it looks through the folder properly?

std::vector<char*> fileArray;

//Get location of program executable
HMODULE hModule = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);

//Remove the executable file name from 'path' so that it refers to the folder instead
PathCchRemoveFileSpec(path, sizeof(path));

//This code should find the first file in the executable folder, but it fails
//Instead, it obtains the name of the folder that it is searching
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(path, &ffd);

do
{
    //The name of the folder is pushed onto the array because of the previous code's mistake
    //e.g. If the folder is "C:\\MyFolder", it stores "MyFolder"
    fileArray.push_back(ffd.cFileName); //Disclaimer: This line of code won't add the file name properly (I'll get to fixing it later), but that's not relevant to the question
} while (FindNextFile(hFind, &ffd) != 0); //This line fails to find anymore files
Remy Lebeau

I have been told that the following code should work

You were told wrong, because the code you have presented is severely broken.

the FindFirstFile operation only finds one file (the name of the folder that it should be searching through).

You are passing just the folder path by itself to FindFirstFile(), so only one entry will be reported, describing the folder itself. What you need to do is append a * or *.* wildcard to the end of the path, then FindFirstFile()/FindNextFile() will enumerate the files and subfolders inside the folder.

Aside from that, there are several other problems with the code.

Even if the enumeration were working, you are not differentiating between files and subfolders.

You are passing a bad value to the second parameter of PathCchRemoveFileSpec(). You are passing in a byte count, but it expects a character count instead.

You are not checking FindFirstFile() for failure before entering your loop, and you are not calling FindClose() after the loop is finished.

And lastly, your code won't even compile as-is. Your vector is storing char* values, but in order to pass a WCHAR[] to FindFirstFile(), UNICODE has to be defined, which means FindFirstFile() will map to FindFirstFileW() and WIN32_FIND_DATA will map to WIN32_FIND_DATAW, and thus the cFileName field will be a WCHAR[]. The compiler will not allow a WCHAR[] (or WCHAR*) to be assigned to a char*.

Try something more like this instead:

std::vector<std::wstring> fileArray;

//Get location of program executable
WCHAR path[MAX_PATH+1] = {0};
GetModuleFileNameW(NULL, path, MAX_PATH);

//Remove the executable file name from 'path' so that it refers to the folder instead
PathCchRemoveFileSpec(path, MAX_PATH);

// append a wildcard to the path
PathCchAppend(path, MAX_PATH, L"*.*");

WIN32_FIND_DATAW ffd;
HANDLE hFind = FindFirstFileW(path, &ffd);
if (hFile != INVALID_HANDLE_VALUE)
{
    do
    {
        if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
            fileArray.push_back(ffd.cFileName);
    }
    while (FindNextFileW(hFind, &ffd));
    FindClose(hFind);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I recursively replace a string in file and directory names?

From Dev

How do I obtain all the row names in a Cassandra table, efficiently?

From Dev

How can I obtain only word without All Punctuation Marks when I read text file?

From Dev

How can I show all file types contained in a directory?

From Dev

How can I print all directory and files and all sub directory and file at java

From Dev

How can I create a CSV file from a directory listing with multiple columns based on the file names?

From Dev

How can I obtain all the image pixels from a UIImage object

From Dev

In Laravel, how can I obtain a list of all files in a public folder?

From Dev

How can I obtain all the image pixels from a UIImage object

From Dev

How can I search all file names for all packages in a given repository on Fedora?

From Dev

How can I replace a string in both directory and file names, using the Ubuntu "rename" tool?

From Dev

How can I obtain a file handle from its fileno in Perl?

From Dev

How to remove `_` from all file-names in a directory

From Dev

How to write out all directory names into a text file

From Dev

How can I get the names of all nodes or polygons from an fbx file?

From Dev

How do I list file names on a server in a directory?

From Dev

How can I format a list of files with directory names in unix?

From Dev

how can i return the names of files in a directory using php

From Dev

PHP How can I iterate over a directory and merge all files that end with same string in a single file of that name

From Dev

how can I find all the file which created after some time, in a given directory?

From Dev

How can I delete all the files in the current directory except one file?

From Dev

How can I save a file in the current directory?

From Dev

How can I remove a file or directory called "\"?

From Dev

how can i copy file in data directory

From Dev

Inno Setup: List all file names in an directory

From Dev

can i place all my .bash* file in to a directory?

From Dev

Can I obtain C++ type names in a constexpr way?

From Dev

how to obtain objects in objects (with all the key names) in SQL?

From Dev

Unix: How can I count all lines containing a string in all files in a directory and see the output for each file separately

Related Related

  1. 1

    How can I recursively replace a string in file and directory names?

  2. 2

    How do I obtain all the row names in a Cassandra table, efficiently?

  3. 3

    How can I obtain only word without All Punctuation Marks when I read text file?

  4. 4

    How can I show all file types contained in a directory?

  5. 5

    How can I print all directory and files and all sub directory and file at java

  6. 6

    How can I create a CSV file from a directory listing with multiple columns based on the file names?

  7. 7

    How can I obtain all the image pixels from a UIImage object

  8. 8

    In Laravel, how can I obtain a list of all files in a public folder?

  9. 9

    How can I obtain all the image pixels from a UIImage object

  10. 10

    How can I search all file names for all packages in a given repository on Fedora?

  11. 11

    How can I replace a string in both directory and file names, using the Ubuntu "rename" tool?

  12. 12

    How can I obtain a file handle from its fileno in Perl?

  13. 13

    How to remove `_` from all file-names in a directory

  14. 14

    How to write out all directory names into a text file

  15. 15

    How can I get the names of all nodes or polygons from an fbx file?

  16. 16

    How do I list file names on a server in a directory?

  17. 17

    How can I format a list of files with directory names in unix?

  18. 18

    how can i return the names of files in a directory using php

  19. 19

    PHP How can I iterate over a directory and merge all files that end with same string in a single file of that name

  20. 20

    how can I find all the file which created after some time, in a given directory?

  21. 21

    How can I delete all the files in the current directory except one file?

  22. 22

    How can I save a file in the current directory?

  23. 23

    How can I remove a file or directory called "\"?

  24. 24

    how can i copy file in data directory

  25. 25

    Inno Setup: List all file names in an directory

  26. 26

    can i place all my .bash* file in to a directory?

  27. 27

    Can I obtain C++ type names in a constexpr way?

  28. 28

    how to obtain objects in objects (with all the key names) in SQL?

  29. 29

    Unix: How can I count all lines containing a string in all files in a directory and see the output for each file separately

HotTag

Archive