Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
include<iostream>
#include<io.h>
#include<string>
#include<fstream>
using namespace std;
void dfsFolder(string folderPath, ostream &fout)
{
    _finddata_t FileInfo;
    //only can read current folder text file .I can't read the text in subfolder;
    //how can I read every text file included in sub-folder by regular expression?
    string strfind = folderPath + "\\*.txt";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);

    if (Handle == -1L)
    {
        cerr << "can not match the folder path" << endl;
        exit(-1);
    }
    do{
        if (FileInfo.attrib & _A_SUBDIR)
        {
            string newpath = strfind + "\\" + FileInfo.name;
            dfsFolder(newpath,fout);
        }
        else
            fout << folderPath << "\\" << FileInfo.name  << endl;
    }while (_findnext(Handle, &FileInfo) == 0);
    _findclose(Handle);
}

int main()
{
    string path;
    cin>>path;
    dfsFolder(path,cout);
}


only can read current folder text file .I can't read the text in subfolder;
how can I read every text file included in sub-folder by regular expression?


who can help me correct it ?
Posted
Updated 17-May-13 0:46am
v3
Comments
P Uday kishore 17-May-13 7:54am    
i did scanning of the same type in mfc.
you can go through this code once.
FindFile(CString strPath, CString strWildCard)
{

CFileFind finder;
CString strTempPath = strPath + "\\" + strWildCard;
BOOL bWorking = finder.FindFile(strTempPath);


while (bWorking)
{

bWorking = finder.FindNextFile();

if (finder.IsDots())
continue;

if (finder.IsDirectory())
{
CString str = finder.GetFilePath();

FindFile(str,strWildCard);
}
else
{
CString strFileName= finder.GetFileName();

}

}
return true;
}

Explanation:here wildcard am using is *.*;

1 solution

You need to read all directory entries and filter out the ones that do not have the .txt extension. Also, when a returned entry is a directory rather than a normal file, you should call your function recursively to process the sub-directory, and by extension, all sub-directories below that. You should also be using this function[^] and its associates.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900