Hi everyone
I'm new to c++ but have had some luck so far. But I have of course met a problem.
The program I'm making is supposed to find a a unkown file with an ID. The onliest thing I know of the file is the folder its in.
so I want to open every file in this folder and read them, to see if they have the ID. But I dont know how to do that.
this is what I got so far:

void FindFile(){

    char pPathFolder[250];
    char pPathCommunicator[251];
    string FolderPath = "\\Documents\\logfiles"; // this is the folder with the file
    string CommunicatorPath = "\\Documents\\FileCommunicator.txt";
    char* pPathInit = getenv ("USERPROFILE");
    char* pPathInitC = getenv ("USERPROFILE");
    strcpy(pPathFolder,pPathInit);
    strcat(pPathFolder,FolderPath.c_str());
    strcpy(pPathCommunicator,pPathInitC);
    strcat(pPathCommunicator,CommunicatorPath.c_str());

    bool FileExists = false;

    if (pPathFolder == NULL) {

        cout << "Folder is NIL (Null)" << endl;

    } else {

        do{

            if (file_exists("file.txt") == true){ // if the file exists

                FileExists = true;

            }

        }
        while (FileExists == false);


        ofstream CommunicatorToFile;
        CommunicatorToFile.open(pPathCommunicator);
        CommunicatorToFile << "logfiles\\test.txt"; // then write the path to the file here
        CommunicatorToFile.close();

    }

}

It looks like you are on MS-Windows, so you can use the win32 api function FindFirstFile() and FindNextFile() to get a list of all the files in the desired folder. They are pretty easy to use, and that link has an example program at the bottom.

thanks for the response, but as usual for me, it wont work.

void FindScarLog(){

    char pPathFolder[250];
    char pPathCommunicator[251];
    string FolderPath = "\\Documents\\logfiles";
    string CommunicatorPath = "\\Documents\\FileCommunicator.txt";
    char* pPathInit = getenv ("USERPROFILE");
    char* pPathInitC = getenv ("USERPROFILE");
    strcpy(pPathFolder,pPathInit);
    strcat(pPathFolder,FolderPath.c_str());
    strcpy(pPathCommunicator,pPathInitC);
    strcat(pPathCommunicator,CommunicatorPath.c_str());

    bool FileExists = false;

    if (pPathFolder == NULL) {

        cout << "Folder is NIL (Null)" << endl;

    } else {

        do{

            WIN32_FIND_DATA FindFileData;
            HANDLE hFind;

            hFind = FindFirstFile(pPathFolder, &FindFileData);
            if (hFind == INVALID_HANDLE_VALUE)
            {
                printf ("FindFirstFile failed (%d)\n", GetLastError());
            }
            else
            {
                do
                {
                    if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                    {
                    }
                    else
                    {
                        cout << FindFileData.cFileName << endl;
                    }
                }
                while (FindNextFile(hFind, &FindFileData));
                FindClose(hFind);
            }

            if (file_exists("File.txt") == true){

                FileExists = true;

            }

        }
        while (FileExists == false);

        ofstream CommunicatorToFile;
        CommunicatorToFile.open(pPathCommunicator);
        CommunicatorToFile << "logfiles\\scarlog-random-date.txt";
        CommunicatorToFile.close();

    }

}

the program runs fine, or it can start. But it wont print the filename
I tried some other code but that only gave me the folder name

The main reason your program doesn't work is because the first parameter to FindFirstFile() is incomplete -- it needs "\*.txt" added to the end of the path.

The following code works, but I've commented out some of your code. Note that when it is looking for specific file extension such as txt there is no need to check for folders attribute unless there are folder names with txt as an extension.

#include <Windows.h>
#include <iostream>
#include <cstring>
#include <string>
using std::cout;
using std::string;
using std::endl;

#pragma warning(disable: 4996)


void FindScarLog(){

    char pPathFolder[250];
    char pPathCommunicator[251];
    string FolderPath = "\\Documents\\logfiles";
    string CommunicatorPath = "\\Documents\\FileCommunicator.txt";
    char* pPathInit = getenv("USERPROFILE");
    char* pPathInitC = getenv("USERPROFILE");
    strcpy(pPathFolder, pPathInit);
    strcat(pPathFolder, FolderPath.c_str());
    strcpy(pPathCommunicator, pPathInitC);
    strcat(pPathCommunicator, CommunicatorPath.c_str());

    bool FileExists = false;

    if (pPathFolder == NULL) {

        cout << "Folder is NIL (Null)" << endl;

    }
    else {

        do{

            WIN32_FIND_DATA FindFileData;
            HANDLE hFind;
            string files = pPathFolder;
            files += "\\*.txt";
            hFind = FindFirstFile(files.c_str(), &FindFileData);
            if (hFind == INVALID_HANDLE_VALUE)
            {
                printf("FindFirstFile failed (%d)\n", GetLastError());
            }
            else
            {
                do
                {
                    cout << FindFileData.cFileName << endl;
                } while (FindNextFile(hFind, &FindFileData));
                FindClose(hFind);
            }

        //  if (file_exists("File.txt") == true){

            //  FileExists = true;

//          }

        } while (FileExists == false);

//      ofstream CommunicatorToFile;
//      CommunicatorToFile.open(pPathCommunicator);
//      CommunicatorToFile << "logfiles\\scarlog-random-date.txt";
//      CommunicatorToFile.close();

    }

}

int main()
{
    FindScarLog();
}

thank you works just fine now, but if I want to search for a string in the file?

You will have to read each file line-by-line and search for the string you want. Do that on line 49 of the code I posted, inside that do loop.

    do
                            {
                                cout << FindFileData.cFileName << endl;

                                string line;
                                ifstream file(FindFileData.cFileName);
                                if (file)
                                {
                                while (getline(file, line))
                                  {
                                  if (line == "Vet-Log-ID = 1")
                                    {
                                        cout << "test" << endl;
                                        FileExists = true;
                                        break;
                                    }
                                  }
                                file.close();
                                }


                            } while
                            (FindNextFile(hFind, &FindFileData));
                            FindClose(hFind);

I'm obviously doing something wrong here, but what am I doing wrong?
I know just what the line I'm looking for says, and I dont care about the other lines.

Is the file in the current working directory? If not then you have to add the full path to it. FindFileData.cFileName does not contain the path.

this is my final code, and it works

void FindLog(){

    char pPathFolder[250];
    char pPathCommunicator[251];
    char pPathInside[252];
    string FolderPath = "\\Documents\\logfiles";
    string CommunicatorPath = "\\Documents\\FileCommunicator.txt";
    string FolderPathInside = "\\Documents\\logfiles\\";
    char* pPathInit = getenv ("USERPROFILE");
    char* pPathInitC = getenv ("USERPROFILE");
    char* pPathInitI = getenv ("USERPROFILE");
    strcpy(pPathFolder,pPathInit);
    strcat(pPathFolder,FolderPath.c_str());
    strcpy(pPathCommunicator,pPathInitC);
    strcat(pPathCommunicator,CommunicatorPath.c_str());
    strcpy(pPathInside,pPathInitI);
    strcat(pPathInside,FolderPathInside.c_str());

    bool LogFileExists = false;

    if (pPathFolder == NULL) {

        cout << "Folder is NIL (Null)" << endl;

    } else {

        do{

            WIN32_FIND_DATA FindFileData;
            HANDLE hFind;

            hFind = FindFirstFile(pPathFolder, &FindFileData);
            if (hFind == INVALID_HANDLE_VALUE)
            {
                printf ("FindFirstFile failed (%d)\n", GetLastError());
            }
            else
            {
                    WIN32_FIND_DATA FindFileData;
                    HANDLE hFind;
                    string files = pPathFolder;
                    files += "\\*.txt";
                    hFind = FindFirstFile(files.c_str(), &FindFileData);

                    if (hFind == INVALID_HANDLE_VALUE)
                    {
                        printf("FindFirstFile failed (%d)\n", GetLastError());
                    }
                    else{
                        do
                        {
                            cout << FindFileData.cFileName << endl;

                            char pFullPath[253];
                            strcpy(pFullPath,pPathInitI);
                            strcat(pFullPath,FolderPathInside.c_str());
                            strcat(pFullPath,FindFileData.cFileName);

                            cout << pFullPath << endl;

                            string line;
                            ifstream myfile(pFullPath);

                            if (myfile)
                            {
                            while (getline( myfile, line ))
                              {
                                if (line == "Vet-Log-ID = 1")
                                {
                                    cout << "test" << endl;
                                    LogFileExists = true;
                                    break;
                                }
                              }
                            myfile.close();
                            }


                        } while
                        (FindNextFile(hFind, &FindFileData));
                        FindClose(hFind);
                    }
            }

        }
        while (LogFileExists == false);

        ofstream CommunicatorToLUAFile;
        CommunicatorToFile.open(pPathCommunicator);
        CommunicatorToFile << "logfiles\\scarlog-random-date.txt";
        CommunicatorToFile.close();

    }

}

thank you for helping me

Suggestion: lines 54-57: It would be easier if you used std::string for pFullPath, then instead of calling strcpy() you could just use the assignment = operator. Then on line 62 just use pFullPath.c_str().

Be a part of the DaniWeb community

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