Question

I have a path like:

C:\path\to\my*file\

and I would like to get the corresponding full path (if it exists):

C:\path\to\my1file\

I tried with this Qt code, but the result is the same path I had at the beginning:

QStringList filters;
filters << "C:/path/to/my*file/";

QDir dir;
dir.setNameFilters(filters);

QStringList dirs = dir.entryList(filters);

_path = dirs.at(0);     // get the first path only

Shouldn't I get all the files/directories that get through the filter?
Why is _path equal to "C:/path/to/my*file/"?

Is it possible to do the same thing with C++98/STL only? (In this project I cannot use Boost/C++11).

Was it helpful?

Solution

Use filters to filter files/folders, and set the path in QDir object:

QStringList filters;
filters << "my*file";

QDir dir("C:/path/to/");
QStringList dirs = dir.entryList(filters);

if (dirs.size() > 0)
{
    qDebug() << dirs.at(0);
}

OTHER TIPS

Expanding file names is called globbing. On Windows the functions FindFirstFile() / FindNextFile() do globbing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top