简体   繁体   中英

Is there any way to check whether a file exist with a file name contains regular expression

Given the path you are search under, and the file name contains regular expression, is there any way to check whether there is any file satisfy the file name pattern exists under this path? In C++?? for example: under path

/path

I want to search the file "foo.*"

foo.* 

the result could be foo.1 or foo.2 or whatever. I would like to get the result(I mean I want to know whether the file exist and the exact file name), Thanks

Look into boost::filesystem , otherwise you will need to look at the api for your os.

Here is an example (untested):

boost::filesystem::path dir("/path");
boost::filesystem::directory_iterator it(dir), end;
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(it, end))
{
    if(boost::filesystem::is_regular_file(p))
    {
        if(p.stem().string() == "foo")
        {
            std::cout << p.extension() << '\n';
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM