How to do not list specified files
-
@eefesafak my bad, this only works with filters of
moc
instead ofmoc*
startWith has no overload for regEx, so you'll have to use containsif(subDirectory.baseName().contains(QRegularExpression("^" + entry) ) );
-
@J-Hilk said in How to do not list specified files:
QRegularExpression("^" + entry)
Won't work correctly.
moc*
etc. are wildcard patterns, not regular expressions, which you are assuming! (E.g. test:moc*
in Exclude list will excludemoc_anything
[correctly but coincidentally] but will also excludemo_anything
, which it should not.)Shall I leave it to you/ @eefesafak to correct? :)
-
@eefesafak said in How to do not list specified files:
@JonB It would be great.
Looks like correct (Qt 5.12+) would be:
if (subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry) ) );
P.S.
OP is Linux, so filenames are case-sensitive and hence this is correct. If you took this to Windows you would want to addQRegularExpression::CaseInsensitiveOption
to theQRegularExpression::PatternOptions
. -
This is my code right now and it is still not working.
QFileInfoList MainWindow::getFileListFromDir(const QString &directory) { QDir qdir(directory); QString include = ui->lineEdit->text(); QString except = ui->lineEdit2->text(); QStringList includeList = include.split(QLatin1Char(';')); QStringList exceptList = except.split(QLatin1Char(';')); QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size); for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { bool doInclude{true}; for(const QString &entry : exceptList) { if(subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry))) { doInclude = false; break; } } if(doInclude) fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } ui->lineEdit->setText(ui->lineEdit->displayText()); return fileList; }
-
@eefesafak said in How to do not list specified files:
QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
sorry, totally my bad. I didn't understand your code correctly.
You solely rely on the
entryInfoList
that you give a filter to to get all your files, so you would have to also give your exceptList to that.However, I don't know how one "negates" a wildcard match 🤷♂️
I would personally iterate myself over the files.
-
@eefesafak said in How to do not list specified files:
This is my code right now and it is still not working.
If you only apply the
exceptList
to yourqdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)
, which only returns directories, how would you expect it to affect the files returned from the separateQFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
??Separately, since you do not apply the
includeList
to the directories why do you choose to apply theexceptList
to them?Do you follow what your own code is doing?
-
@JonB said in How to do not list specified files:
Separately, since you do not apply the includeList to the directories why do you choose to apply the exceptList to them?
Thanks for reminding. My program is working correctly. Here is my code:
QFileInfoList MainWindow::getFileListFromDir(const QString &directory) { QDir qdir(directory); QString include = ui->lineEdit->text(); QString except = ui->lineEdit2->text(); QStringList includeList = include.split(QLatin1Char(';')); QStringList exceptList = except.split(QLatin1Char(';')); QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size); for(const QFileInfo& file : qdir.entryInfoList(QStringList() << exceptList)) { for(const QFileInfo& filee: fileList) { if(file.fileName() == filee.fileName()) { fileList.removeOne(filee); } } } for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } ui->lineEdit->setText(ui->lineEdit->displayText()); return fileList; }