hi everbody
I was opening all files in a certain folder I've used WIN32_FIND_DATA, FindFirstFile, FindNextFile. it works well but for certain extension that i ask the user to enter
i.e it will get only all the files with the extension given by he user and ignore any thing else (files with other extensions or sub folders )
so I was asking if there was a way to deal with all the files in the same manner (not mention the extesion ??? but how to deal with the file with any fstream in this case????????) and also by the way what if I want to get the files in a sub folder that exist in the specified folder (I want to travel through any folder till find a file )
????

thanks in advance

ZlapX commented: sucks balls +0

Recommended Answers

All 3 Replies

If you want to deal with subfolders then you will have to start by passing *.* to FindFirst() so that it will return all files and foldes. Then your program will have to filter out the ones it wants. There have been several code snippets that illustrate how to do that -- I even posted one on DaniWeb

@Ancient Dragon
I've searched on and tried what you have told me in the last post (useFindFirstFile("*.*", ) ) but it get everything in the current directory(the directory where the compiler's exe is found ) and when i tried to change it as I found on msdn it gives me errors
I'm using Dev-C++ 4.9.9.2
here is the code that i've till now :

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <direct.h> 
using namespace std;
using namespace System;
using namespace System::IO;
int main(int argc, char *argv[])
{char dir[40];
  //string^ dir="c:\\pics";
  strcpy(dir, "c:\\pics");
  Directory::SetCurrentDirectory( dir );
   
//string path = "c:\\pics\\";

   //string searchPattern = "*.jpg";
string path;
//cout<<"Enter the path of the folder u want"<<endl;
//cin>>path;
    string fullSearchPath = path ;//+ searchPattern;

char FilePath[_MAX_PATH];
getcwd(FilePath, _MAX_PATH);
 // and just to see if getcwd worked...
cout << FilePath;

   WIN32_FIND_DATA FindData;

    HANDLE hFind;
    //hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );

  hFind = FindFirstFile("*.*", &FindData );

    if( hFind == INVALID_HANDLE_VALUE )

    {
        cout << "Error searching directory\n";
        system("PAUSE");
        return -1;

    }
int count=0;
   do

    {
        count++;
        cout<<"the file no. "<<count<<" is "<<FindData.cFileName<<endl;
        cout<<"its size in bytes is "<<FindData.nFileSizeLow<<endl;
        cout<<"**************************"<<endl;
        string filePath = path + FindData.cFileName;

        ifstream in( filePath.c_str() );

        if( in )

        {

            // do stuff with the file here

        }

        else

        {

            cout << "Problem opening file " << FindData.cFileName << "\n";

        }

    }

    while( FindNextFile(hFind, &FindData) > 0 );
    if( GetLastError() != ERROR_NO_MORE_FILES )

    {

        cout << "Something went wrong during searching\n";

    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

and the errors are:
8 C:\Dev-cpp\FindFirstfile77.cpp expected namespace-name before ';' token
8 C:\Dev-cpp\FindFirstfile77.cpp `<type error>' is not a namespace
9 C:\Dev-cpp\FindFirstfile77.cpp `System' has not been declared
14 C:\Dev-cpp\FindFirstfile77.cpp `Directory' has not been declared

if there is any easier way than changing the current directory please tell me(I've searched on your previous posts but didnot get to an example using FindFirstFile("*.*"))
and as usual thanks so much .

lines 8 and 9 are not pure c++, they are for managed code, which Dev-C++ can not handle.

Here is an example of how to do it. The function uses recursion to transverse the subdirectories. It builds a vector of all the file names. Feel free to change it any way you wish to adapt it to your problem.

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.