How do I get a list of files in a directory in C++? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to get list of folders in this folder?How to list files in a directory using the Windows API?how to get file names vs directory names in c++ (using boost filesystem library)How to read the mp3 files in a given folder?C++ multiple files with common name beginningOpen an .exe file without knowing the full path in C++reading .txt file's 2nd line and write to another .txt file - C++C++ search a file name containing a certain extention/wordBenchmarking on the 2006 Middlebury Stereo DatasetGet the source directory of a Bash script from within the script itselfHow do I check whether a file exists without exceptions?How can I add an empty directory to a Git repository?The Definitive C++ Book Guide and ListHow do I include a JavaScript file in another JavaScript file?What is the “-->” operator in C++?How do I list all files of a directory?How to read a file line-by-line into a list?Find current directory and file's directoryHow do I find all files containing specific text on Linux?

What were wait-states, and why was it only an issue for PCs?

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

What's parked in Mil Moscow helicopter plant?

Is it OK if I do not take the receipt in Germany?

What happened to Viserion in Season 7?

How to translate "red flag" into Spanish?

Is Bran literally the world's memory?

/bin/ls sorts differently than just ls

Is a self contained air-bullet cartridge feasible?

Processing ADC conversion result: DMA vs Processor Registers

Was Objective-C really a hindrance to Apple software development?

Why aren't road bicycle wheels tiny?

What does the black goddess statue do and what is it?

Marquee sign letters

Will I be more secure with my own router behind my ISP's router?

Why is water being consumed when my shutoff valve is closed?

What *exactly* is electrical current, voltage, and resistance?

Why do people think Winterfell crypts is the safest place for women, children & old people?

A journey... into the MIND

What is the evidence that custom checks in Northern Ireland are going to result in violence?

How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?

When does Bran Stark remember Jamie pushing him?

Will I lose my paid in full property

Test if all elements of a Foldable are the same



How do I get a list of files in a directory in C++?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to get list of folders in this folder?How to list files in a directory using the Windows API?how to get file names vs directory names in c++ (using boost filesystem library)How to read the mp3 files in a given folder?C++ multiple files with common name beginningOpen an .exe file without knowing the full path in C++reading .txt file's 2nd line and write to another .txt file - C++C++ search a file name containing a certain extention/wordBenchmarking on the 2006 Middlebury Stereo DatasetGet the source directory of a Bash script from within the script itselfHow do I check whether a file exists without exceptions?How can I add an empty directory to a Git repository?The Definitive C++ Book Guide and ListHow do I include a JavaScript file in another JavaScript file?What is the “-->” operator in C++?How do I list all files of a directory?How to read a file line-by-line into a list?Find current directory and file's directoryHow do I find all files containing specific text on Linux?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








44















How do you get a list of files within a directory so each can be processed?










share|improve this question




























    44















    How do you get a list of files within a directory so each can be processed?










    share|improve this question
























      44












      44








      44


      20






      How do you get a list of files within a directory so each can be processed?










      share|improve this question














      How do you get a list of files within a directory so each can be processed?







      c++ file directory






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '08 at 19:23









      DShookDShook

      9,63163549




      9,63163549






















          13 Answers
          13






          active

          oldest

          votes


















          35














          But boost::filesystem can do that: http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp






          share|improve this answer
































            58














            Here's what I use:



            /* Returns a list of files in a directory (except the ones that begin with a dot) */

            void GetFilesInDirectory(std::vector<string> &out, const string &directory)

            #ifdef WINDOWS
            HANDLE dir;
            WIN32_FIND_DATA file_data;

            if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
            return; /* No files found */

            do
            const string file_name = file_data.cFileName;
            const string full_file_name = directory + "/" + file_name;
            const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

            if (file_name[0] == '.')
            continue;

            if (is_directory)
            continue;

            out.push_back(full_file_name);
            while (FindNextFile(dir, &file_data));

            FindClose(dir);
            #else
            DIR *dir;
            class dirent *ent;
            class stat st;

            dir = opendir(directory);
            while ((ent = readdir(dir)) != NULL)
            const string file_name = ent->d_name;
            const string full_file_name = directory + "/" + file_name;

            if (file_name[0] == '.')
            continue;

            if (stat(full_file_name.c_str(), &st) == -1)
            continue;

            const bool is_directory = (st.st_mode & S_IFDIR) != 0;

            if (is_directory)
            continue;

            out.push_back(full_file_name);

            closedir(dir);
            #endif
            // GetFilesInDirectory





            share|improve this answer

























            • The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

              – Joe
              Dec 18 '15 at 19:29






            • 5





              Which headers do you include for this to work?

              – emlai
              Apr 23 '16 at 12:40











            • Can't use dirent if you are making a lib.

              – Katianie
              May 25 '16 at 22:37






            • 1





              @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

              – Cedric
              Mar 22 '17 at 2:45



















            25














            Here's an example in C on Linux. That's if, you're on Linux and don't mind doing this small bit in ANSI C.



            #include <dirent.h>

            DIR *dpdf;
            struct dirent *epdf;

            dpdf = opendir("./");
            if (dpdf != NULL)
            while (epdf = readdir(dpdf))
            printf("Filename: %s",epdf->d_name);
            // std::cout << epdf->d_name << std::endl;


            closedir(dpdf);





            share|improve this answer




















            • 11





              don't forget to closedir(dpdf) afterwards

              – jsj
              Aug 7 '15 at 2:25











            • how can i get a file (FILE type) in this case? epdf is a "dirent *"

              – Alex Goft
              Apr 5 '16 at 11:05











            • Can't use dirent if you are making a lib.

              – Katianie
              May 25 '16 at 22:36






            • 1





              What do dpdf and epdf stand for?

              – byxor
              Mar 7 '18 at 9:53






            • 1





              I was working with PDFs at the time. Those names could be anything you like.

              – Chris Kloberdanz
              Mar 8 '18 at 20:55



















            4














            You have to use operating system calls (e.g. the Win32 API) or a wrapper around them. I tend to use Boost.Filesystem as it is superior interface compared to the mess that is the Win32 API (as well as being cross platform).



            If you are looking to use the Win32 API, Microsoft has a list of functions and examples on msdn.






            share|improve this answer






























              3














              If you're in Windows & using MSVC, the MSDN library has sample code that does this.



              And here's the code from that link:



              #include <windows.h>
              #include <tchar.h>
              #include <stdio.h>
              #include <strsafe.h>

              void ErrorHandler(LPTSTR lpszFunction);

              int _tmain(int argc, TCHAR *argv[])

              WIN32_FIND_DATA ffd;
              LARGE_INTEGER filesize;
              TCHAR szDir[MAX_PATH];
              size_t length_of_arg;
              HANDLE hFind = INVALID_HANDLE_VALUE;
              DWORD dwError=0;

              // If the directory is not specified as a command-line argument,
              // print usage.

              if(argc != 2)

              _tprintf(TEXT("nUsage: %s <directory name>n"), argv[0]);
              return (-1);


              // Check that the input path plus 2 is not longer than MAX_PATH.

              StringCchLength(argv[1], MAX_PATH, &length_of_arg);

              if (length_of_arg > (MAX_PATH - 2))

              _tprintf(TEXT("nDirectory path is too long.n"));
              return (-1);


              _tprintf(TEXT("nTarget directory is %snn"), argv[1]);

              // Prepare string for use with FindFile functions. First, copy the
              // string to a buffer, then append '*' to the directory name.

              StringCchCopy(szDir, MAX_PATH, argv[1]);
              StringCchCat(szDir, MAX_PATH, TEXT("\*"));

              // Find the first file in the directory.

              hFind = FindFirstFile(szDir, &ffd);

              if (INVALID_HANDLE_VALUE == hFind)

              ErrorHandler(TEXT("FindFirstFile"));
              return dwError;


              // List all the files in the directory with some info about them.

              do

              if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

              _tprintf(TEXT(" %s <DIR>n"), ffd.cFileName);

              else

              filesize.LowPart = ffd.nFileSizeLow;
              filesize.HighPart = ffd.nFileSizeHigh;
              _tprintf(TEXT(" %s %ld bytesn"), ffd.cFileName, filesize.QuadPart);


              while (FindNextFile(hFind, &ffd) != 0);

              dwError = GetLastError();
              if (dwError != ERROR_NO_MORE_FILES)

              ErrorHandler(TEXT("FindFirstFile"));


              FindClose(hFind);
              return dwError;



              void ErrorHandler(LPTSTR lpszFunction)

              FORMAT_MESSAGE_IGNORE_INSERTS,
              NULL,
              dw,
              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
              (LPTSTR) &lpMsgBuf,
              0, NULL );

              // Display the error message and exit the process

              lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
              (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
              StringCchPrintf((LPTSTR)lpDisplayBuf,
              LocalSize(lpDisplayBuf) / sizeof(TCHAR),
              TEXT("%s failed with error %d: %s"),
              lpszFunction, dw, lpMsgBuf);
              MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

              LocalFree(lpMsgBuf);
              LocalFree(lpDisplayBuf);






              share|improve this answer






























                3














                C++11/Linux version:



                #include <dirent.h>

                if (auto dir = opendir("some_dir/"))
                while (auto f = readdir(dir))
                closedir(dir);






                share|improve this answer






























                  2














                  Solving this will require a platform specific solution. Look for opendir() on unix/linux or FindFirstFile() on Windows. Or, there are many libraries that will handle the platform specific part for you.






                  share|improve this answer
































                    2














                    I've just asked a similar question and here's my solution based on answer received (using boost::filesystem library):



                    #include <string>
                    #include <iostream>
                    #include <boost/filesystem.hpp>
                    using namespace std;
                    using namespace boost::filesystem;

                    int main()

                    path p("D:/AnyFolder");
                    for (auto i = directory_iterator(p); i != directory_iterator(); i++)

                    if (!is_directory(i->path())) //we eliminate directories in a list

                    cout << i->path().filename().string() << endl;

                    else
                    continue;




                    Output is like:



                    file1.txt
                    file2.dat





                    share|improve this answer




















                    • 1





                      Your answer is the best...

                      – user1098761
                      May 31 '17 at 6:25


















                    1














                    After combining a lot of snippets, I finally found a reuseable solution for Windows, that uses ATL Library, which comes with Visual Studio.



                    #include <atlstr.h>

                    void getFiles(CString directory)
                    HANDLE dir;
                    WIN32_FIND_DATA file_data;
                    CString file_name, full_file_name;
                    if ((dir = FindFirstFile((directory + "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                    // Invalid directory


                    while (FindNextFile(dir, &file_data))
                    file_name = file_data.cFileName;
                    full_file_name = directory + file_name;
                    if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                    std::string fileName = full_file_name.GetString();
                    // Do stuff with fileName





                    To access the method, just call:



                    getFiles("i:\Folder1");





                    share|improve this answer

























                    • Can you mention if this is for Windows or Linux or both?

                      – Nadav B
                      Jan 10 '18 at 11:19






                    • 1





                      @NadavB it uses atlstr.h library, which I believe is only for Windows.

                      – Jean Knapp
                      Jun 17 '18 at 20:57


















                    0














                    Or you do this and then read out the test.txt:



                    #include <windows.h>

                    int main()
                    system("dir /b > test.txt");



                    The "/b" means just filenames are returned, no further info.






                    share|improve this answer

























                    • This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                      – smttsp
                      Jul 27 '13 at 11:26


















                    0














                    HANDLE WINAPI FindFirstFile(
                    __in LPCTSTR lpFileName,
                    __out LPWIN32_FIND_DATA lpFindFileData
                    );


                    Setup the attributes to only look for directories.






                    share|improve this answer
































                      0














                      You can use the following code for getting all files in a directory.A simple modification in the Andreas Bonini answer to remove the occurance of "." and ".."



                      CString dirpath="d:\mydir"
                      DWORD errVal = ERROR_SUCCESS;
                      HANDLE dir;
                      WIN32_FIND_DATA file_data;
                      CString file_name,full_file_name;
                      if ((dir = FindFirstFile((dirname+ "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                      errVal=ERROR_INVALID_ACCEL_HANDLE;
                      return errVal;


                      while (FindNextFile(dir, &file_data))
                      file_name = file_data.cFileName;
                      full_file_name = dirname+ file_name;
                      if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                      m_List.AddTail(full_file_name);







                      share|improve this answer
































                        -1














                        void getFilesList(String filePath,String extension, vector<string> & returnFileName)

                        WIN32_FIND_DATA fileInfo;
                        HANDLE hFind;
                        String fullPath = filePath + extension;
                        hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
                        if (hFind == INVALID_HANDLE_VALUE)return;
                        else
                        return FileName.push_back(filePath+fileInfo.cFileName);
                        while (FindNextFile(hFind, &fileInfo) != 0)
                        return FileName.push_back(filePath+fileInfo.cFileName);




                        String optfileName ="";
                        String inputFolderPath ="";
                        String extension = "*.jpg*";
                        getFilesList(inputFolderPath,extension,filesPaths);
                        vector<string>::const_iterator it = filesPaths.begin();
                        while( it != filesPaths.end())

                        frame = imread(*it);//read file names
                        //doyourwork here ( frame );
                        sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
                        imwrite(buf,frame);
                        it++;






                        share|improve this answer

























                        • you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                          – mmohab
                          May 6 '14 at 23:50











                        Your Answer






                        StackExchange.ifUsing("editor", function ()
                        StackExchange.using("externalEditor", function ()
                        StackExchange.using("snippets", function ()
                        StackExchange.snippets.init();
                        );
                        );
                        , "code-snippets");

                        StackExchange.ready(function()
                        var channelOptions =
                        tags: "".split(" "),
                        id: "1"
                        ;
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function()
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled)
                        StackExchange.using("snippets", function()
                        createEditor();
                        );

                        else
                        createEditor();

                        );

                        function createEditor()
                        StackExchange.prepareEditor(
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader:
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        ,
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        );



                        );













                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function ()
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f306533%2fhow-do-i-get-a-list-of-files-in-a-directory-in-c%23new-answer', 'question_page');

                        );

                        Post as a guest















                        Required, but never shown

























                        13 Answers
                        13






                        active

                        oldest

                        votes








                        13 Answers
                        13






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        35














                        But boost::filesystem can do that: http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp






                        share|improve this answer





























                          35














                          But boost::filesystem can do that: http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp






                          share|improve this answer



























                            35












                            35








                            35







                            But boost::filesystem can do that: http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp






                            share|improve this answer















                            But boost::filesystem can do that: http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 22 at 14:54









                            Community

                            11




                            11










                            answered Nov 20 '08 at 19:24









                            Johannes Schaub - litbJohannes Schaub - litb

                            412k1027861115




                            412k1027861115























                                58














                                Here's what I use:



                                /* Returns a list of files in a directory (except the ones that begin with a dot) */

                                void GetFilesInDirectory(std::vector<string> &out, const string &directory)

                                #ifdef WINDOWS
                                HANDLE dir;
                                WIN32_FIND_DATA file_data;

                                if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
                                return; /* No files found */

                                do
                                const string file_name = file_data.cFileName;
                                const string full_file_name = directory + "/" + file_name;
                                const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

                                if (file_name[0] == '.')
                                continue;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);
                                while (FindNextFile(dir, &file_data));

                                FindClose(dir);
                                #else
                                DIR *dir;
                                class dirent *ent;
                                class stat st;

                                dir = opendir(directory);
                                while ((ent = readdir(dir)) != NULL)
                                const string file_name = ent->d_name;
                                const string full_file_name = directory + "/" + file_name;

                                if (file_name[0] == '.')
                                continue;

                                if (stat(full_file_name.c_str(), &st) == -1)
                                continue;

                                const bool is_directory = (st.st_mode & S_IFDIR) != 0;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);

                                closedir(dir);
                                #endif
                                // GetFilesInDirectory





                                share|improve this answer

























                                • The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

                                  – Joe
                                  Dec 18 '15 at 19:29






                                • 5





                                  Which headers do you include for this to work?

                                  – emlai
                                  Apr 23 '16 at 12:40











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:37






                                • 1





                                  @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

                                  – Cedric
                                  Mar 22 '17 at 2:45
















                                58














                                Here's what I use:



                                /* Returns a list of files in a directory (except the ones that begin with a dot) */

                                void GetFilesInDirectory(std::vector<string> &out, const string &directory)

                                #ifdef WINDOWS
                                HANDLE dir;
                                WIN32_FIND_DATA file_data;

                                if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
                                return; /* No files found */

                                do
                                const string file_name = file_data.cFileName;
                                const string full_file_name = directory + "/" + file_name;
                                const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

                                if (file_name[0] == '.')
                                continue;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);
                                while (FindNextFile(dir, &file_data));

                                FindClose(dir);
                                #else
                                DIR *dir;
                                class dirent *ent;
                                class stat st;

                                dir = opendir(directory);
                                while ((ent = readdir(dir)) != NULL)
                                const string file_name = ent->d_name;
                                const string full_file_name = directory + "/" + file_name;

                                if (file_name[0] == '.')
                                continue;

                                if (stat(full_file_name.c_str(), &st) == -1)
                                continue;

                                const bool is_directory = (st.st_mode & S_IFDIR) != 0;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);

                                closedir(dir);
                                #endif
                                // GetFilesInDirectory





                                share|improve this answer

























                                • The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

                                  – Joe
                                  Dec 18 '15 at 19:29






                                • 5





                                  Which headers do you include for this to work?

                                  – emlai
                                  Apr 23 '16 at 12:40











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:37






                                • 1





                                  @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

                                  – Cedric
                                  Mar 22 '17 at 2:45














                                58












                                58








                                58







                                Here's what I use:



                                /* Returns a list of files in a directory (except the ones that begin with a dot) */

                                void GetFilesInDirectory(std::vector<string> &out, const string &directory)

                                #ifdef WINDOWS
                                HANDLE dir;
                                WIN32_FIND_DATA file_data;

                                if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
                                return; /* No files found */

                                do
                                const string file_name = file_data.cFileName;
                                const string full_file_name = directory + "/" + file_name;
                                const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

                                if (file_name[0] == '.')
                                continue;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);
                                while (FindNextFile(dir, &file_data));

                                FindClose(dir);
                                #else
                                DIR *dir;
                                class dirent *ent;
                                class stat st;

                                dir = opendir(directory);
                                while ((ent = readdir(dir)) != NULL)
                                const string file_name = ent->d_name;
                                const string full_file_name = directory + "/" + file_name;

                                if (file_name[0] == '.')
                                continue;

                                if (stat(full_file_name.c_str(), &st) == -1)
                                continue;

                                const bool is_directory = (st.st_mode & S_IFDIR) != 0;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);

                                closedir(dir);
                                #endif
                                // GetFilesInDirectory





                                share|improve this answer















                                Here's what I use:



                                /* Returns a list of files in a directory (except the ones that begin with a dot) */

                                void GetFilesInDirectory(std::vector<string> &out, const string &directory)

                                #ifdef WINDOWS
                                HANDLE dir;
                                WIN32_FIND_DATA file_data;

                                if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
                                return; /* No files found */

                                do
                                const string file_name = file_data.cFileName;
                                const string full_file_name = directory + "/" + file_name;
                                const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

                                if (file_name[0] == '.')
                                continue;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);
                                while (FindNextFile(dir, &file_data));

                                FindClose(dir);
                                #else
                                DIR *dir;
                                class dirent *ent;
                                class stat st;

                                dir = opendir(directory);
                                while ((ent = readdir(dir)) != NULL)
                                const string file_name = ent->d_name;
                                const string full_file_name = directory + "/" + file_name;

                                if (file_name[0] == '.')
                                continue;

                                if (stat(full_file_name.c_str(), &st) == -1)
                                continue;

                                const bool is_directory = (st.st_mode & S_IFDIR) != 0;

                                if (is_directory)
                                continue;

                                out.push_back(full_file_name);

                                closedir(dir);
                                #endif
                                // GetFilesInDirectory






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 18 '13 at 11:19









                                lahjaton_j

                                526411




                                526411










                                answered Dec 19 '09 at 12:58









                                Thomas BoniniThomas Bonini

                                30k27114146




                                30k27114146












                                • The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

                                  – Joe
                                  Dec 18 '15 at 19:29






                                • 5





                                  Which headers do you include for this to work?

                                  – emlai
                                  Apr 23 '16 at 12:40











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:37






                                • 1





                                  @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

                                  – Cedric
                                  Mar 22 '17 at 2:45


















                                • The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

                                  – Joe
                                  Dec 18 '15 at 19:29






                                • 5





                                  Which headers do you include for this to work?

                                  – emlai
                                  Apr 23 '16 at 12:40











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:37






                                • 1





                                  @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

                                  – Cedric
                                  Mar 22 '17 at 2:45

















                                The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

                                – Joe
                                Dec 18 '15 at 19:29





                                The Windows code doesn't quite work when compiled with Unicode enabled. I got around this by explicitly calling the ASCII functions, which have an 'A' appended. Specifically, use WIN32_FIND_DATAA, FindFirstFileA, and FindNextFileA and everything works. Obviously this is a hack, but I live in an English speaking country so it works for me. :0 Thanks for the example code!

                                – Joe
                                Dec 18 '15 at 19:29




                                5




                                5





                                Which headers do you include for this to work?

                                – emlai
                                Apr 23 '16 at 12:40





                                Which headers do you include for this to work?

                                – emlai
                                Apr 23 '16 at 12:40













                                Can't use dirent if you are making a lib.

                                – Katianie
                                May 25 '16 at 22:37





                                Can't use dirent if you are making a lib.

                                – Katianie
                                May 25 '16 at 22:37




                                1




                                1





                                @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

                                – Cedric
                                Mar 22 '17 at 2:45






                                @tuple_cat <dirent.h>, <sys/stat.h> and for sure some kind of file that includes std::string. Using std::string, you have to put std:: in front of the string type declaration and use opendir(directory.c_str()).

                                – Cedric
                                Mar 22 '17 at 2:45












                                25














                                Here's an example in C on Linux. That's if, you're on Linux and don't mind doing this small bit in ANSI C.



                                #include <dirent.h>

                                DIR *dpdf;
                                struct dirent *epdf;

                                dpdf = opendir("./");
                                if (dpdf != NULL)
                                while (epdf = readdir(dpdf))
                                printf("Filename: %s",epdf->d_name);
                                // std::cout << epdf->d_name << std::endl;


                                closedir(dpdf);





                                share|improve this answer




















                                • 11





                                  don't forget to closedir(dpdf) afterwards

                                  – jsj
                                  Aug 7 '15 at 2:25











                                • how can i get a file (FILE type) in this case? epdf is a "dirent *"

                                  – Alex Goft
                                  Apr 5 '16 at 11:05











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:36






                                • 1





                                  What do dpdf and epdf stand for?

                                  – byxor
                                  Mar 7 '18 at 9:53






                                • 1





                                  I was working with PDFs at the time. Those names could be anything you like.

                                  – Chris Kloberdanz
                                  Mar 8 '18 at 20:55
















                                25














                                Here's an example in C on Linux. That's if, you're on Linux and don't mind doing this small bit in ANSI C.



                                #include <dirent.h>

                                DIR *dpdf;
                                struct dirent *epdf;

                                dpdf = opendir("./");
                                if (dpdf != NULL)
                                while (epdf = readdir(dpdf))
                                printf("Filename: %s",epdf->d_name);
                                // std::cout << epdf->d_name << std::endl;


                                closedir(dpdf);





                                share|improve this answer




















                                • 11





                                  don't forget to closedir(dpdf) afterwards

                                  – jsj
                                  Aug 7 '15 at 2:25











                                • how can i get a file (FILE type) in this case? epdf is a "dirent *"

                                  – Alex Goft
                                  Apr 5 '16 at 11:05











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:36






                                • 1





                                  What do dpdf and epdf stand for?

                                  – byxor
                                  Mar 7 '18 at 9:53






                                • 1





                                  I was working with PDFs at the time. Those names could be anything you like.

                                  – Chris Kloberdanz
                                  Mar 8 '18 at 20:55














                                25












                                25








                                25







                                Here's an example in C on Linux. That's if, you're on Linux and don't mind doing this small bit in ANSI C.



                                #include <dirent.h>

                                DIR *dpdf;
                                struct dirent *epdf;

                                dpdf = opendir("./");
                                if (dpdf != NULL)
                                while (epdf = readdir(dpdf))
                                printf("Filename: %s",epdf->d_name);
                                // std::cout << epdf->d_name << std::endl;


                                closedir(dpdf);





                                share|improve this answer















                                Here's an example in C on Linux. That's if, you're on Linux and don't mind doing this small bit in ANSI C.



                                #include <dirent.h>

                                DIR *dpdf;
                                struct dirent *epdf;

                                dpdf = opendir("./");
                                if (dpdf != NULL)
                                while (epdf = readdir(dpdf))
                                printf("Filename: %s",epdf->d_name);
                                // std::cout << epdf->d_name << std::endl;


                                closedir(dpdf);






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Oct 5 '17 at 17:58









                                Antonio

                                12k644145




                                12k644145










                                answered Nov 20 '08 at 21:30









                                Chris KloberdanzChris Kloberdanz

                                2,99142630




                                2,99142630







                                • 11





                                  don't forget to closedir(dpdf) afterwards

                                  – jsj
                                  Aug 7 '15 at 2:25











                                • how can i get a file (FILE type) in this case? epdf is a "dirent *"

                                  – Alex Goft
                                  Apr 5 '16 at 11:05











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:36






                                • 1





                                  What do dpdf and epdf stand for?

                                  – byxor
                                  Mar 7 '18 at 9:53






                                • 1





                                  I was working with PDFs at the time. Those names could be anything you like.

                                  – Chris Kloberdanz
                                  Mar 8 '18 at 20:55













                                • 11





                                  don't forget to closedir(dpdf) afterwards

                                  – jsj
                                  Aug 7 '15 at 2:25











                                • how can i get a file (FILE type) in this case? epdf is a "dirent *"

                                  – Alex Goft
                                  Apr 5 '16 at 11:05











                                • Can't use dirent if you are making a lib.

                                  – Katianie
                                  May 25 '16 at 22:36






                                • 1





                                  What do dpdf and epdf stand for?

                                  – byxor
                                  Mar 7 '18 at 9:53






                                • 1





                                  I was working with PDFs at the time. Those names could be anything you like.

                                  – Chris Kloberdanz
                                  Mar 8 '18 at 20:55








                                11




                                11





                                don't forget to closedir(dpdf) afterwards

                                – jsj
                                Aug 7 '15 at 2:25





                                don't forget to closedir(dpdf) afterwards

                                – jsj
                                Aug 7 '15 at 2:25













                                how can i get a file (FILE type) in this case? epdf is a "dirent *"

                                – Alex Goft
                                Apr 5 '16 at 11:05





                                how can i get a file (FILE type) in this case? epdf is a "dirent *"

                                – Alex Goft
                                Apr 5 '16 at 11:05













                                Can't use dirent if you are making a lib.

                                – Katianie
                                May 25 '16 at 22:36





                                Can't use dirent if you are making a lib.

                                – Katianie
                                May 25 '16 at 22:36




                                1




                                1





                                What do dpdf and epdf stand for?

                                – byxor
                                Mar 7 '18 at 9:53





                                What do dpdf and epdf stand for?

                                – byxor
                                Mar 7 '18 at 9:53




                                1




                                1





                                I was working with PDFs at the time. Those names could be anything you like.

                                – Chris Kloberdanz
                                Mar 8 '18 at 20:55






                                I was working with PDFs at the time. Those names could be anything you like.

                                – Chris Kloberdanz
                                Mar 8 '18 at 20:55












                                4














                                You have to use operating system calls (e.g. the Win32 API) or a wrapper around them. I tend to use Boost.Filesystem as it is superior interface compared to the mess that is the Win32 API (as well as being cross platform).



                                If you are looking to use the Win32 API, Microsoft has a list of functions and examples on msdn.






                                share|improve this answer



























                                  4














                                  You have to use operating system calls (e.g. the Win32 API) or a wrapper around them. I tend to use Boost.Filesystem as it is superior interface compared to the mess that is the Win32 API (as well as being cross platform).



                                  If you are looking to use the Win32 API, Microsoft has a list of functions and examples on msdn.






                                  share|improve this answer

























                                    4












                                    4








                                    4







                                    You have to use operating system calls (e.g. the Win32 API) or a wrapper around them. I tend to use Boost.Filesystem as it is superior interface compared to the mess that is the Win32 API (as well as being cross platform).



                                    If you are looking to use the Win32 API, Microsoft has a list of functions and examples on msdn.






                                    share|improve this answer













                                    You have to use operating system calls (e.g. the Win32 API) or a wrapper around them. I tend to use Boost.Filesystem as it is superior interface compared to the mess that is the Win32 API (as well as being cross platform).



                                    If you are looking to use the Win32 API, Microsoft has a list of functions and examples on msdn.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 19 '09 at 12:56









                                    YacobyYacoby

                                    45.3k1299114




                                    45.3k1299114





















                                        3














                                        If you're in Windows & using MSVC, the MSDN library has sample code that does this.



                                        And here's the code from that link:



                                        #include <windows.h>
                                        #include <tchar.h>
                                        #include <stdio.h>
                                        #include <strsafe.h>

                                        void ErrorHandler(LPTSTR lpszFunction);

                                        int _tmain(int argc, TCHAR *argv[])

                                        WIN32_FIND_DATA ffd;
                                        LARGE_INTEGER filesize;
                                        TCHAR szDir[MAX_PATH];
                                        size_t length_of_arg;
                                        HANDLE hFind = INVALID_HANDLE_VALUE;
                                        DWORD dwError=0;

                                        // If the directory is not specified as a command-line argument,
                                        // print usage.

                                        if(argc != 2)

                                        _tprintf(TEXT("nUsage: %s <directory name>n"), argv[0]);
                                        return (-1);


                                        // Check that the input path plus 2 is not longer than MAX_PATH.

                                        StringCchLength(argv[1], MAX_PATH, &length_of_arg);

                                        if (length_of_arg > (MAX_PATH - 2))

                                        _tprintf(TEXT("nDirectory path is too long.n"));
                                        return (-1);


                                        _tprintf(TEXT("nTarget directory is %snn"), argv[1]);

                                        // Prepare string for use with FindFile functions. First, copy the
                                        // string to a buffer, then append '*' to the directory name.

                                        StringCchCopy(szDir, MAX_PATH, argv[1]);
                                        StringCchCat(szDir, MAX_PATH, TEXT("\*"));

                                        // Find the first file in the directory.

                                        hFind = FindFirstFile(szDir, &ffd);

                                        if (INVALID_HANDLE_VALUE == hFind)

                                        ErrorHandler(TEXT("FindFirstFile"));
                                        return dwError;


                                        // List all the files in the directory with some info about them.

                                        do

                                        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

                                        _tprintf(TEXT(" %s <DIR>n"), ffd.cFileName);

                                        else

                                        filesize.LowPart = ffd.nFileSizeLow;
                                        filesize.HighPart = ffd.nFileSizeHigh;
                                        _tprintf(TEXT(" %s %ld bytesn"), ffd.cFileName, filesize.QuadPart);


                                        while (FindNextFile(hFind, &ffd) != 0);

                                        dwError = GetLastError();
                                        if (dwError != ERROR_NO_MORE_FILES)

                                        ErrorHandler(TEXT("FindFirstFile"));


                                        FindClose(hFind);
                                        return dwError;



                                        void ErrorHandler(LPTSTR lpszFunction)

                                        FORMAT_MESSAGE_IGNORE_INSERTS,
                                        NULL,
                                        dw,
                                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                        (LPTSTR) &lpMsgBuf,
                                        0, NULL );

                                        // Display the error message and exit the process

                                        lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                                        (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
                                        StringCchPrintf((LPTSTR)lpDisplayBuf,
                                        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                                        TEXT("%s failed with error %d: %s"),
                                        lpszFunction, dw, lpMsgBuf);
                                        MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

                                        LocalFree(lpMsgBuf);
                                        LocalFree(lpDisplayBuf);






                                        share|improve this answer



























                                          3














                                          If you're in Windows & using MSVC, the MSDN library has sample code that does this.



                                          And here's the code from that link:



                                          #include <windows.h>
                                          #include <tchar.h>
                                          #include <stdio.h>
                                          #include <strsafe.h>

                                          void ErrorHandler(LPTSTR lpszFunction);

                                          int _tmain(int argc, TCHAR *argv[])

                                          WIN32_FIND_DATA ffd;
                                          LARGE_INTEGER filesize;
                                          TCHAR szDir[MAX_PATH];
                                          size_t length_of_arg;
                                          HANDLE hFind = INVALID_HANDLE_VALUE;
                                          DWORD dwError=0;

                                          // If the directory is not specified as a command-line argument,
                                          // print usage.

                                          if(argc != 2)

                                          _tprintf(TEXT("nUsage: %s <directory name>n"), argv[0]);
                                          return (-1);


                                          // Check that the input path plus 2 is not longer than MAX_PATH.

                                          StringCchLength(argv[1], MAX_PATH, &length_of_arg);

                                          if (length_of_arg > (MAX_PATH - 2))

                                          _tprintf(TEXT("nDirectory path is too long.n"));
                                          return (-1);


                                          _tprintf(TEXT("nTarget directory is %snn"), argv[1]);

                                          // Prepare string for use with FindFile functions. First, copy the
                                          // string to a buffer, then append '*' to the directory name.

                                          StringCchCopy(szDir, MAX_PATH, argv[1]);
                                          StringCchCat(szDir, MAX_PATH, TEXT("\*"));

                                          // Find the first file in the directory.

                                          hFind = FindFirstFile(szDir, &ffd);

                                          if (INVALID_HANDLE_VALUE == hFind)

                                          ErrorHandler(TEXT("FindFirstFile"));
                                          return dwError;


                                          // List all the files in the directory with some info about them.

                                          do

                                          if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

                                          _tprintf(TEXT(" %s <DIR>n"), ffd.cFileName);

                                          else

                                          filesize.LowPart = ffd.nFileSizeLow;
                                          filesize.HighPart = ffd.nFileSizeHigh;
                                          _tprintf(TEXT(" %s %ld bytesn"), ffd.cFileName, filesize.QuadPart);


                                          while (FindNextFile(hFind, &ffd) != 0);

                                          dwError = GetLastError();
                                          if (dwError != ERROR_NO_MORE_FILES)

                                          ErrorHandler(TEXT("FindFirstFile"));


                                          FindClose(hFind);
                                          return dwError;



                                          void ErrorHandler(LPTSTR lpszFunction)

                                          FORMAT_MESSAGE_IGNORE_INSERTS,
                                          NULL,
                                          dw,
                                          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                          (LPTSTR) &lpMsgBuf,
                                          0, NULL );

                                          // Display the error message and exit the process

                                          lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                                          (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
                                          StringCchPrintf((LPTSTR)lpDisplayBuf,
                                          LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                                          TEXT("%s failed with error %d: %s"),
                                          lpszFunction, dw, lpMsgBuf);
                                          MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

                                          LocalFree(lpMsgBuf);
                                          LocalFree(lpDisplayBuf);






                                          share|improve this answer

























                                            3












                                            3








                                            3







                                            If you're in Windows & using MSVC, the MSDN library has sample code that does this.



                                            And here's the code from that link:



                                            #include <windows.h>
                                            #include <tchar.h>
                                            #include <stdio.h>
                                            #include <strsafe.h>

                                            void ErrorHandler(LPTSTR lpszFunction);

                                            int _tmain(int argc, TCHAR *argv[])

                                            WIN32_FIND_DATA ffd;
                                            LARGE_INTEGER filesize;
                                            TCHAR szDir[MAX_PATH];
                                            size_t length_of_arg;
                                            HANDLE hFind = INVALID_HANDLE_VALUE;
                                            DWORD dwError=0;

                                            // If the directory is not specified as a command-line argument,
                                            // print usage.

                                            if(argc != 2)

                                            _tprintf(TEXT("nUsage: %s <directory name>n"), argv[0]);
                                            return (-1);


                                            // Check that the input path plus 2 is not longer than MAX_PATH.

                                            StringCchLength(argv[1], MAX_PATH, &length_of_arg);

                                            if (length_of_arg > (MAX_PATH - 2))

                                            _tprintf(TEXT("nDirectory path is too long.n"));
                                            return (-1);


                                            _tprintf(TEXT("nTarget directory is %snn"), argv[1]);

                                            // Prepare string for use with FindFile functions. First, copy the
                                            // string to a buffer, then append '*' to the directory name.

                                            StringCchCopy(szDir, MAX_PATH, argv[1]);
                                            StringCchCat(szDir, MAX_PATH, TEXT("\*"));

                                            // Find the first file in the directory.

                                            hFind = FindFirstFile(szDir, &ffd);

                                            if (INVALID_HANDLE_VALUE == hFind)

                                            ErrorHandler(TEXT("FindFirstFile"));
                                            return dwError;


                                            // List all the files in the directory with some info about them.

                                            do

                                            if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

                                            _tprintf(TEXT(" %s <DIR>n"), ffd.cFileName);

                                            else

                                            filesize.LowPart = ffd.nFileSizeLow;
                                            filesize.HighPart = ffd.nFileSizeHigh;
                                            _tprintf(TEXT(" %s %ld bytesn"), ffd.cFileName, filesize.QuadPart);


                                            while (FindNextFile(hFind, &ffd) != 0);

                                            dwError = GetLastError();
                                            if (dwError != ERROR_NO_MORE_FILES)

                                            ErrorHandler(TEXT("FindFirstFile"));


                                            FindClose(hFind);
                                            return dwError;



                                            void ErrorHandler(LPTSTR lpszFunction)

                                            FORMAT_MESSAGE_IGNORE_INSERTS,
                                            NULL,
                                            dw,
                                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                            (LPTSTR) &lpMsgBuf,
                                            0, NULL );

                                            // Display the error message and exit the process

                                            lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                                            (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
                                            StringCchPrintf((LPTSTR)lpDisplayBuf,
                                            LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                                            TEXT("%s failed with error %d: %s"),
                                            lpszFunction, dw, lpMsgBuf);
                                            MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

                                            LocalFree(lpMsgBuf);
                                            LocalFree(lpDisplayBuf);






                                            share|improve this answer













                                            If you're in Windows & using MSVC, the MSDN library has sample code that does this.



                                            And here's the code from that link:



                                            #include <windows.h>
                                            #include <tchar.h>
                                            #include <stdio.h>
                                            #include <strsafe.h>

                                            void ErrorHandler(LPTSTR lpszFunction);

                                            int _tmain(int argc, TCHAR *argv[])

                                            WIN32_FIND_DATA ffd;
                                            LARGE_INTEGER filesize;
                                            TCHAR szDir[MAX_PATH];
                                            size_t length_of_arg;
                                            HANDLE hFind = INVALID_HANDLE_VALUE;
                                            DWORD dwError=0;

                                            // If the directory is not specified as a command-line argument,
                                            // print usage.

                                            if(argc != 2)

                                            _tprintf(TEXT("nUsage: %s <directory name>n"), argv[0]);
                                            return (-1);


                                            // Check that the input path plus 2 is not longer than MAX_PATH.

                                            StringCchLength(argv[1], MAX_PATH, &length_of_arg);

                                            if (length_of_arg > (MAX_PATH - 2))

                                            _tprintf(TEXT("nDirectory path is too long.n"));
                                            return (-1);


                                            _tprintf(TEXT("nTarget directory is %snn"), argv[1]);

                                            // Prepare string for use with FindFile functions. First, copy the
                                            // string to a buffer, then append '*' to the directory name.

                                            StringCchCopy(szDir, MAX_PATH, argv[1]);
                                            StringCchCat(szDir, MAX_PATH, TEXT("\*"));

                                            // Find the first file in the directory.

                                            hFind = FindFirstFile(szDir, &ffd);

                                            if (INVALID_HANDLE_VALUE == hFind)

                                            ErrorHandler(TEXT("FindFirstFile"));
                                            return dwError;


                                            // List all the files in the directory with some info about them.

                                            do

                                            if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

                                            _tprintf(TEXT(" %s <DIR>n"), ffd.cFileName);

                                            else

                                            filesize.LowPart = ffd.nFileSizeLow;
                                            filesize.HighPart = ffd.nFileSizeHigh;
                                            _tprintf(TEXT(" %s %ld bytesn"), ffd.cFileName, filesize.QuadPart);


                                            while (FindNextFile(hFind, &ffd) != 0);

                                            dwError = GetLastError();
                                            if (dwError != ERROR_NO_MORE_FILES)

                                            ErrorHandler(TEXT("FindFirstFile"));


                                            FindClose(hFind);
                                            return dwError;



                                            void ErrorHandler(LPTSTR lpszFunction)

                                            FORMAT_MESSAGE_IGNORE_INSERTS,
                                            NULL,
                                            dw,
                                            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                            (LPTSTR) &lpMsgBuf,
                                            0, NULL );

                                            // Display the error message and exit the process

                                            lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                                            (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
                                            StringCchPrintf((LPTSTR)lpDisplayBuf,
                                            LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                                            TEXT("%s failed with error %d: %s"),
                                            lpszFunction, dw, lpMsgBuf);
                                            MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

                                            LocalFree(lpMsgBuf);
                                            LocalFree(lpDisplayBuf);







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Nov 20 '08 at 22:19









                                            John DiblingJohn Dibling

                                            82k21151277




                                            82k21151277





















                                                3














                                                C++11/Linux version:



                                                #include <dirent.h>

                                                if (auto dir = opendir("some_dir/"))
                                                while (auto f = readdir(dir))
                                                closedir(dir);






                                                share|improve this answer



























                                                  3














                                                  C++11/Linux version:



                                                  #include <dirent.h>

                                                  if (auto dir = opendir("some_dir/"))
                                                  while (auto f = readdir(dir))
                                                  closedir(dir);






                                                  share|improve this answer

























                                                    3












                                                    3








                                                    3







                                                    C++11/Linux version:



                                                    #include <dirent.h>

                                                    if (auto dir = opendir("some_dir/"))
                                                    while (auto f = readdir(dir))
                                                    closedir(dir);






                                                    share|improve this answer













                                                    C++11/Linux version:



                                                    #include <dirent.h>

                                                    if (auto dir = opendir("some_dir/"))
                                                    while (auto f = readdir(dir))
                                                    closedir(dir);







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Sep 7 '17 at 22:00









                                                    AdrianEddyAdrianEddy

                                                    3981313




                                                    3981313





















                                                        2














                                                        Solving this will require a platform specific solution. Look for opendir() on unix/linux or FindFirstFile() on Windows. Or, there are many libraries that will handle the platform specific part for you.






                                                        share|improve this answer





























                                                          2














                                                          Solving this will require a platform specific solution. Look for opendir() on unix/linux or FindFirstFile() on Windows. Or, there are many libraries that will handle the platform specific part for you.






                                                          share|improve this answer



























                                                            2












                                                            2








                                                            2







                                                            Solving this will require a platform specific solution. Look for opendir() on unix/linux or FindFirstFile() on Windows. Or, there are many libraries that will handle the platform specific part for you.






                                                            share|improve this answer















                                                            Solving this will require a platform specific solution. Look for opendir() on unix/linux or FindFirstFile() on Windows. Or, there are many libraries that will handle the platform specific part for you.







                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Nov 20 '08 at 21:18

























                                                            answered Nov 20 '08 at 19:25









                                                            Roland RabienRoland Rabien

                                                            6,46673962




                                                            6,46673962





















                                                                2














                                                                I've just asked a similar question and here's my solution based on answer received (using boost::filesystem library):



                                                                #include <string>
                                                                #include <iostream>
                                                                #include <boost/filesystem.hpp>
                                                                using namespace std;
                                                                using namespace boost::filesystem;

                                                                int main()

                                                                path p("D:/AnyFolder");
                                                                for (auto i = directory_iterator(p); i != directory_iterator(); i++)

                                                                if (!is_directory(i->path())) //we eliminate directories in a list

                                                                cout << i->path().filename().string() << endl;

                                                                else
                                                                continue;




                                                                Output is like:



                                                                file1.txt
                                                                file2.dat





                                                                share|improve this answer




















                                                                • 1





                                                                  Your answer is the best...

                                                                  – user1098761
                                                                  May 31 '17 at 6:25















                                                                2














                                                                I've just asked a similar question and here's my solution based on answer received (using boost::filesystem library):



                                                                #include <string>
                                                                #include <iostream>
                                                                #include <boost/filesystem.hpp>
                                                                using namespace std;
                                                                using namespace boost::filesystem;

                                                                int main()

                                                                path p("D:/AnyFolder");
                                                                for (auto i = directory_iterator(p); i != directory_iterator(); i++)

                                                                if (!is_directory(i->path())) //we eliminate directories in a list

                                                                cout << i->path().filename().string() << endl;

                                                                else
                                                                continue;




                                                                Output is like:



                                                                file1.txt
                                                                file2.dat





                                                                share|improve this answer




















                                                                • 1





                                                                  Your answer is the best...

                                                                  – user1098761
                                                                  May 31 '17 at 6:25













                                                                2












                                                                2








                                                                2







                                                                I've just asked a similar question and here's my solution based on answer received (using boost::filesystem library):



                                                                #include <string>
                                                                #include <iostream>
                                                                #include <boost/filesystem.hpp>
                                                                using namespace std;
                                                                using namespace boost::filesystem;

                                                                int main()

                                                                path p("D:/AnyFolder");
                                                                for (auto i = directory_iterator(p); i != directory_iterator(); i++)

                                                                if (!is_directory(i->path())) //we eliminate directories in a list

                                                                cout << i->path().filename().string() << endl;

                                                                else
                                                                continue;




                                                                Output is like:



                                                                file1.txt
                                                                file2.dat





                                                                share|improve this answer















                                                                I've just asked a similar question and here's my solution based on answer received (using boost::filesystem library):



                                                                #include <string>
                                                                #include <iostream>
                                                                #include <boost/filesystem.hpp>
                                                                using namespace std;
                                                                using namespace boost::filesystem;

                                                                int main()

                                                                path p("D:/AnyFolder");
                                                                for (auto i = directory_iterator(p); i != directory_iterator(); i++)

                                                                if (!is_directory(i->path())) //we eliminate directories in a list

                                                                cout << i->path().filename().string() << endl;

                                                                else
                                                                continue;




                                                                Output is like:



                                                                file1.txt
                                                                file2.dat






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited May 23 '17 at 12:10









                                                                Community

                                                                11




                                                                11










                                                                answered Jun 25 '15 at 16:26









                                                                BadBad

                                                                2,61422136




                                                                2,61422136







                                                                • 1





                                                                  Your answer is the best...

                                                                  – user1098761
                                                                  May 31 '17 at 6:25












                                                                • 1





                                                                  Your answer is the best...

                                                                  – user1098761
                                                                  May 31 '17 at 6:25







                                                                1




                                                                1





                                                                Your answer is the best...

                                                                – user1098761
                                                                May 31 '17 at 6:25





                                                                Your answer is the best...

                                                                – user1098761
                                                                May 31 '17 at 6:25











                                                                1














                                                                After combining a lot of snippets, I finally found a reuseable solution for Windows, that uses ATL Library, which comes with Visual Studio.



                                                                #include <atlstr.h>

                                                                void getFiles(CString directory)
                                                                HANDLE dir;
                                                                WIN32_FIND_DATA file_data;
                                                                CString file_name, full_file_name;
                                                                if ((dir = FindFirstFile((directory + "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                // Invalid directory


                                                                while (FindNextFile(dir, &file_data))
                                                                file_name = file_data.cFileName;
                                                                full_file_name = directory + file_name;
                                                                if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                std::string fileName = full_file_name.GetString();
                                                                // Do stuff with fileName





                                                                To access the method, just call:



                                                                getFiles("i:\Folder1");





                                                                share|improve this answer

























                                                                • Can you mention if this is for Windows or Linux or both?

                                                                  – Nadav B
                                                                  Jan 10 '18 at 11:19






                                                                • 1





                                                                  @NadavB it uses atlstr.h library, which I believe is only for Windows.

                                                                  – Jean Knapp
                                                                  Jun 17 '18 at 20:57















                                                                1














                                                                After combining a lot of snippets, I finally found a reuseable solution for Windows, that uses ATL Library, which comes with Visual Studio.



                                                                #include <atlstr.h>

                                                                void getFiles(CString directory)
                                                                HANDLE dir;
                                                                WIN32_FIND_DATA file_data;
                                                                CString file_name, full_file_name;
                                                                if ((dir = FindFirstFile((directory + "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                // Invalid directory


                                                                while (FindNextFile(dir, &file_data))
                                                                file_name = file_data.cFileName;
                                                                full_file_name = directory + file_name;
                                                                if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                std::string fileName = full_file_name.GetString();
                                                                // Do stuff with fileName





                                                                To access the method, just call:



                                                                getFiles("i:\Folder1");





                                                                share|improve this answer

























                                                                • Can you mention if this is for Windows or Linux or both?

                                                                  – Nadav B
                                                                  Jan 10 '18 at 11:19






                                                                • 1





                                                                  @NadavB it uses atlstr.h library, which I believe is only for Windows.

                                                                  – Jean Knapp
                                                                  Jun 17 '18 at 20:57













                                                                1












                                                                1








                                                                1







                                                                After combining a lot of snippets, I finally found a reuseable solution for Windows, that uses ATL Library, which comes with Visual Studio.



                                                                #include <atlstr.h>

                                                                void getFiles(CString directory)
                                                                HANDLE dir;
                                                                WIN32_FIND_DATA file_data;
                                                                CString file_name, full_file_name;
                                                                if ((dir = FindFirstFile((directory + "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                // Invalid directory


                                                                while (FindNextFile(dir, &file_data))
                                                                file_name = file_data.cFileName;
                                                                full_file_name = directory + file_name;
                                                                if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                std::string fileName = full_file_name.GetString();
                                                                // Do stuff with fileName





                                                                To access the method, just call:



                                                                getFiles("i:\Folder1");





                                                                share|improve this answer















                                                                After combining a lot of snippets, I finally found a reuseable solution for Windows, that uses ATL Library, which comes with Visual Studio.



                                                                #include <atlstr.h>

                                                                void getFiles(CString directory)
                                                                HANDLE dir;
                                                                WIN32_FIND_DATA file_data;
                                                                CString file_name, full_file_name;
                                                                if ((dir = FindFirstFile((directory + "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                // Invalid directory


                                                                while (FindNextFile(dir, &file_data))
                                                                file_name = file_data.cFileName;
                                                                full_file_name = directory + file_name;
                                                                if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                std::string fileName = full_file_name.GetString();
                                                                // Do stuff with fileName





                                                                To access the method, just call:



                                                                getFiles("i:\Folder1");






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Jun 17 '18 at 20:55

























                                                                answered Jun 19 '16 at 14:53









                                                                Jean KnappJean Knapp

                                                                112




                                                                112












                                                                • Can you mention if this is for Windows or Linux or both?

                                                                  – Nadav B
                                                                  Jan 10 '18 at 11:19






                                                                • 1





                                                                  @NadavB it uses atlstr.h library, which I believe is only for Windows.

                                                                  – Jean Knapp
                                                                  Jun 17 '18 at 20:57

















                                                                • Can you mention if this is for Windows or Linux or both?

                                                                  – Nadav B
                                                                  Jan 10 '18 at 11:19






                                                                • 1





                                                                  @NadavB it uses atlstr.h library, which I believe is only for Windows.

                                                                  – Jean Knapp
                                                                  Jun 17 '18 at 20:57
















                                                                Can you mention if this is for Windows or Linux or both?

                                                                – Nadav B
                                                                Jan 10 '18 at 11:19





                                                                Can you mention if this is for Windows or Linux or both?

                                                                – Nadav B
                                                                Jan 10 '18 at 11:19




                                                                1




                                                                1





                                                                @NadavB it uses atlstr.h library, which I believe is only for Windows.

                                                                – Jean Knapp
                                                                Jun 17 '18 at 20:57





                                                                @NadavB it uses atlstr.h library, which I believe is only for Windows.

                                                                – Jean Knapp
                                                                Jun 17 '18 at 20:57











                                                                0














                                                                Or you do this and then read out the test.txt:



                                                                #include <windows.h>

                                                                int main()
                                                                system("dir /b > test.txt");



                                                                The "/b" means just filenames are returned, no further info.






                                                                share|improve this answer

























                                                                • This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                                                                  – smttsp
                                                                  Jul 27 '13 at 11:26















                                                                0














                                                                Or you do this and then read out the test.txt:



                                                                #include <windows.h>

                                                                int main()
                                                                system("dir /b > test.txt");



                                                                The "/b" means just filenames are returned, no further info.






                                                                share|improve this answer

























                                                                • This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                                                                  – smttsp
                                                                  Jul 27 '13 at 11:26













                                                                0












                                                                0








                                                                0







                                                                Or you do this and then read out the test.txt:



                                                                #include <windows.h>

                                                                int main()
                                                                system("dir /b > test.txt");



                                                                The "/b" means just filenames are returned, no further info.






                                                                share|improve this answer















                                                                Or you do this and then read out the test.txt:



                                                                #include <windows.h>

                                                                int main()
                                                                system("dir /b > test.txt");



                                                                The "/b" means just filenames are returned, no further info.







                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Sep 13 '13 at 7:47









                                                                Community

                                                                11




                                                                11










                                                                answered Jul 2 '13 at 23:41









                                                                EndersEnders

                                                                91




                                                                91












                                                                • This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                                                                  – smttsp
                                                                  Jul 27 '13 at 11:26

















                                                                • This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                                                                  – smttsp
                                                                  Jul 27 '13 at 11:26
















                                                                This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                                                                – smttsp
                                                                Jul 27 '13 at 11:26





                                                                This looks really nice and easy, but I don't get how to use this. Could you explain what is /b and where will we give the directory?

                                                                – smttsp
                                                                Jul 27 '13 at 11:26











                                                                0














                                                                HANDLE WINAPI FindFirstFile(
                                                                __in LPCTSTR lpFileName,
                                                                __out LPWIN32_FIND_DATA lpFindFileData
                                                                );


                                                                Setup the attributes to only look for directories.






                                                                share|improve this answer





























                                                                  0














                                                                  HANDLE WINAPI FindFirstFile(
                                                                  __in LPCTSTR lpFileName,
                                                                  __out LPWIN32_FIND_DATA lpFindFileData
                                                                  );


                                                                  Setup the attributes to only look for directories.






                                                                  share|improve this answer



























                                                                    0












                                                                    0








                                                                    0







                                                                    HANDLE WINAPI FindFirstFile(
                                                                    __in LPCTSTR lpFileName,
                                                                    __out LPWIN32_FIND_DATA lpFindFileData
                                                                    );


                                                                    Setup the attributes to only look for directories.






                                                                    share|improve this answer















                                                                    HANDLE WINAPI FindFirstFile(
                                                                    __in LPCTSTR lpFileName,
                                                                    __out LPWIN32_FIND_DATA lpFindFileData
                                                                    );


                                                                    Setup the attributes to only look for directories.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Sep 13 '13 at 7:48









                                                                    Mat

                                                                    168k29324349




                                                                    168k29324349










                                                                    answered Dec 19 '09 at 12:59









                                                                    kennykenny

                                                                    14.7k54378




                                                                    14.7k54378





















                                                                        0














                                                                        You can use the following code for getting all files in a directory.A simple modification in the Andreas Bonini answer to remove the occurance of "." and ".."



                                                                        CString dirpath="d:\mydir"
                                                                        DWORD errVal = ERROR_SUCCESS;
                                                                        HANDLE dir;
                                                                        WIN32_FIND_DATA file_data;
                                                                        CString file_name,full_file_name;
                                                                        if ((dir = FindFirstFile((dirname+ "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                        errVal=ERROR_INVALID_ACCEL_HANDLE;
                                                                        return errVal;


                                                                        while (FindNextFile(dir, &file_data))
                                                                        file_name = file_data.cFileName;
                                                                        full_file_name = dirname+ file_name;
                                                                        if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                        m_List.AddTail(full_file_name);







                                                                        share|improve this answer





























                                                                          0














                                                                          You can use the following code for getting all files in a directory.A simple modification in the Andreas Bonini answer to remove the occurance of "." and ".."



                                                                          CString dirpath="d:\mydir"
                                                                          DWORD errVal = ERROR_SUCCESS;
                                                                          HANDLE dir;
                                                                          WIN32_FIND_DATA file_data;
                                                                          CString file_name,full_file_name;
                                                                          if ((dir = FindFirstFile((dirname+ "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                          errVal=ERROR_INVALID_ACCEL_HANDLE;
                                                                          return errVal;


                                                                          while (FindNextFile(dir, &file_data))
                                                                          file_name = file_data.cFileName;
                                                                          full_file_name = dirname+ file_name;
                                                                          if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                          m_List.AddTail(full_file_name);







                                                                          share|improve this answer



























                                                                            0












                                                                            0








                                                                            0







                                                                            You can use the following code for getting all files in a directory.A simple modification in the Andreas Bonini answer to remove the occurance of "." and ".."



                                                                            CString dirpath="d:\mydir"
                                                                            DWORD errVal = ERROR_SUCCESS;
                                                                            HANDLE dir;
                                                                            WIN32_FIND_DATA file_data;
                                                                            CString file_name,full_file_name;
                                                                            if ((dir = FindFirstFile((dirname+ "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                            errVal=ERROR_INVALID_ACCEL_HANDLE;
                                                                            return errVal;


                                                                            while (FindNextFile(dir, &file_data))
                                                                            file_name = file_data.cFileName;
                                                                            full_file_name = dirname+ file_name;
                                                                            if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                            m_List.AddTail(full_file_name);







                                                                            share|improve this answer















                                                                            You can use the following code for getting all files in a directory.A simple modification in the Andreas Bonini answer to remove the occurance of "." and ".."



                                                                            CString dirpath="d:\mydir"
                                                                            DWORD errVal = ERROR_SUCCESS;
                                                                            HANDLE dir;
                                                                            WIN32_FIND_DATA file_data;
                                                                            CString file_name,full_file_name;
                                                                            if ((dir = FindFirstFile((dirname+ "/*"), &file_data)) == INVALID_HANDLE_VALUE)

                                                                            errVal=ERROR_INVALID_ACCEL_HANDLE;
                                                                            return errVal;


                                                                            while (FindNextFile(dir, &file_data))
                                                                            file_name = file_data.cFileName;
                                                                            full_file_name = dirname+ file_name;
                                                                            if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)

                                                                            m_List.AddTail(full_file_name);








                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited Jul 3 '14 at 9:56

























                                                                            answered Jul 3 '14 at 9:50









                                                                            tjdoubtstjdoubts

                                                                            64




                                                                            64





















                                                                                -1














                                                                                void getFilesList(String filePath,String extension, vector<string> & returnFileName)

                                                                                WIN32_FIND_DATA fileInfo;
                                                                                HANDLE hFind;
                                                                                String fullPath = filePath + extension;
                                                                                hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
                                                                                if (hFind == INVALID_HANDLE_VALUE)return;
                                                                                else
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);
                                                                                while (FindNextFile(hFind, &fileInfo) != 0)
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);




                                                                                String optfileName ="";
                                                                                String inputFolderPath ="";
                                                                                String extension = "*.jpg*";
                                                                                getFilesList(inputFolderPath,extension,filesPaths);
                                                                                vector<string>::const_iterator it = filesPaths.begin();
                                                                                while( it != filesPaths.end())

                                                                                frame = imread(*it);//read file names
                                                                                //doyourwork here ( frame );
                                                                                sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
                                                                                imwrite(buf,frame);
                                                                                it++;






                                                                                share|improve this answer

























                                                                                • you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                                                                                  – mmohab
                                                                                  May 6 '14 at 23:50















                                                                                -1














                                                                                void getFilesList(String filePath,String extension, vector<string> & returnFileName)

                                                                                WIN32_FIND_DATA fileInfo;
                                                                                HANDLE hFind;
                                                                                String fullPath = filePath + extension;
                                                                                hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
                                                                                if (hFind == INVALID_HANDLE_VALUE)return;
                                                                                else
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);
                                                                                while (FindNextFile(hFind, &fileInfo) != 0)
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);




                                                                                String optfileName ="";
                                                                                String inputFolderPath ="";
                                                                                String extension = "*.jpg*";
                                                                                getFilesList(inputFolderPath,extension,filesPaths);
                                                                                vector<string>::const_iterator it = filesPaths.begin();
                                                                                while( it != filesPaths.end())

                                                                                frame = imread(*it);//read file names
                                                                                //doyourwork here ( frame );
                                                                                sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
                                                                                imwrite(buf,frame);
                                                                                it++;






                                                                                share|improve this answer

























                                                                                • you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                                                                                  – mmohab
                                                                                  May 6 '14 at 23:50













                                                                                -1












                                                                                -1








                                                                                -1







                                                                                void getFilesList(String filePath,String extension, vector<string> & returnFileName)

                                                                                WIN32_FIND_DATA fileInfo;
                                                                                HANDLE hFind;
                                                                                String fullPath = filePath + extension;
                                                                                hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
                                                                                if (hFind == INVALID_HANDLE_VALUE)return;
                                                                                else
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);
                                                                                while (FindNextFile(hFind, &fileInfo) != 0)
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);




                                                                                String optfileName ="";
                                                                                String inputFolderPath ="";
                                                                                String extension = "*.jpg*";
                                                                                getFilesList(inputFolderPath,extension,filesPaths);
                                                                                vector<string>::const_iterator it = filesPaths.begin();
                                                                                while( it != filesPaths.end())

                                                                                frame = imread(*it);//read file names
                                                                                //doyourwork here ( frame );
                                                                                sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
                                                                                imwrite(buf,frame);
                                                                                it++;






                                                                                share|improve this answer















                                                                                void getFilesList(String filePath,String extension, vector<string> & returnFileName)

                                                                                WIN32_FIND_DATA fileInfo;
                                                                                HANDLE hFind;
                                                                                String fullPath = filePath + extension;
                                                                                hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
                                                                                if (hFind == INVALID_HANDLE_VALUE)return;
                                                                                else
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);
                                                                                while (FindNextFile(hFind, &fileInfo) != 0)
                                                                                return FileName.push_back(filePath+fileInfo.cFileName);




                                                                                String optfileName ="";
                                                                                String inputFolderPath ="";
                                                                                String extension = "*.jpg*";
                                                                                getFilesList(inputFolderPath,extension,filesPaths);
                                                                                vector<string>::const_iterator it = filesPaths.begin();
                                                                                while( it != filesPaths.end())

                                                                                frame = imread(*it);//read file names
                                                                                //doyourwork here ( frame );
                                                                                sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
                                                                                imwrite(buf,frame);
                                                                                it++;







                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited May 6 '14 at 23:51









                                                                                mmohab

                                                                                1,45332038




                                                                                1,45332038










                                                                                answered May 6 '14 at 23:28









                                                                                samsam

                                                                                8117




                                                                                8117












                                                                                • you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                                                                                  – mmohab
                                                                                  May 6 '14 at 23:50

















                                                                                • you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                                                                                  – mmohab
                                                                                  May 6 '14 at 23:50
















                                                                                you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                                                                                – mmohab
                                                                                May 6 '14 at 23:50





                                                                                you have 2 returns in method getFilesList, the code after the return FileName.push_back(filePath+fileInfo.cFileName); is not reachable

                                                                                – mmohab
                                                                                May 6 '14 at 23:50

















                                                                                draft saved

                                                                                draft discarded
















































                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid


                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.

                                                                                To learn more, see our tips on writing great answers.




                                                                                draft saved


                                                                                draft discarded














                                                                                StackExchange.ready(
                                                                                function ()
                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f306533%2fhow-do-i-get-a-list-of-files-in-a-directory-in-c%23new-answer', 'question_page');

                                                                                );

                                                                                Post as a guest















                                                                                Required, but never shown





















































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown

































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown







                                                                                Popular posts from this blog

                                                                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                                                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                                                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript