I need help changing my return type from int to vector<string>.. I don't want to have to pass the vector as a pointer to the function but rather I just want to return the vector itself as I no longer need to check the return type. If it fails, it'd return an empty vector.

I was thinking to declare a vector inside the function and the loop the function's internals but then I got stuck at the If Success check because it starts the function all over again.

I changed it from StringArray type to a vector of strings so it's compilable for anyone helping.

#define Repeat do{
#define Until(condition) }while(!(condition));

#include <string>
#include <vector>
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

int SearchDirectory(vector<string> &FilesFound, string RootDirectory, string FileExtension, bool SearchSubdirectories = false)
    string  FilePath;                   // Filepath
    string  Pattern;                    // Pattern
    string  Extension;                  // Extension
    HANDLE  hFile;                      // Handle to file
    WIN32_FIND_DATA FileInformation;    // File information

    Pattern = RootDirectory + "/*.*";

    hFile = FindFirstFile(Pattern.c_str(), &FileInformation);
        Repeat //A simple do-while loop with an opposite condition.. Ported from pascal.
            if(FileInformation.cFileName[0] != '.')
                FilePath = RootDirectory + "/" + FileInformation.cFileName;

                if(FileInformation.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
                        // Search subdirectory
                        int Success = SearchDirectory(FilesFound, FilePath, FileExtension, SearchSubdirectories);     //Recursive call.. Not sure if I'll overflow the stack here.. Any better ways to do this?
                            return Success;
                    //Check extension
                    Extension = FileInformation.cFileName;
                    Extension = Extension.substr(Extension.rfind(".") + 1); //Find the last dot as that is the extension..

                    if(Extension == FileExtension)
                        FilesFound.push_back(FilePath);  //Add file to StringArray.
        Until(FindNextFile(hFile, &FileInformation) != TRUE);

        FindClose(hFile);   //CloseFile Handle

        DWORD dwError = GetLastError();
        if(dwError != ERROR_NO_MORE_FILES)
            return dwError;
    return 0;

int main()
    vector<string> X;    //StringArray X; Changed to vector so others can compile it.
    SearchDirectory(X, "C:/Testing", "dat", true);
    for (unsigned I = 0; I < X.size(); I++)

you have 2 options:
1- use pointer and return Null and check on it
2-return vector<string> and check on size and for recursive part:

// Search subdirectory
vector<string> tmp = SearchDirectory(FilePath, FileExtension, SearchSubdirectories);     //Recursive call.. Not sure if I'll overflow the stack here.. Any better ways to do this?
for(int i=0;i<tmp.size();i++)
commented: Epic! Flawless post :D +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.