How to do not list specified files
-
I can already list specified files. In addition, my goal is to do not list files starting with a specific name.
The program and 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 &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } ui->lineEdit->setText(ui->lineEdit->displayText()); return fileList; }
On the program image, I can list files ending with specific name (*.cpp and *.o) but for example, I do not want to list moc_countline.o
Thanks for your help. -
I can already list specified files. In addition, my goal is to do not list files starting with a specific name.
The program and 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 &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } ui->lineEdit->setText(ui->lineEdit->displayText()); return fileList; }
On the program image, I can list files ending with specific name (*.cpp and *.o) but for example, I do not want to list moc_countline.o
Thanks for your help.for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { bool doInclude{true}; for(const QString &entry : exceptList) { if(subDirectory.baseName().startsWith(entry)) { //alternative to startsWith() -> contains() - decide for yourself what to use doInclude = false; break; } } if(doInclude) fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } -
for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) { bool doInclude{true}; for(const QString &entry : exceptList) { if(subDirectory.baseName().startsWith(entry)) { //alternative to startsWith() -> contains() - decide for yourself what to use doInclude = false; break; } } if(doInclude) fileList << getFileListFromDir(subDirectory.absoluteFilePath()); } -
@J-Hilk I suppose this codes work only for subdirectories. Becaues, I did not get the output I want.
@eefesafak my bad, this only works with filters of
mocinstead ofmoc*
startWith has no overload for regEx, so you'll have to use containsif(subDirectory.baseName().contains(QRegularExpression("^" + entry) ) );
-
@eefesafak my bad, this only works with filters of
mocinstead 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? :)
-
@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? :)
-
@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::CaseInsensitiveOptionto theQRegularExpression::PatternOptions. -
@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::CaseInsensitiveOptionto 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; }
-
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
entryInfoListthat 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.
-
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:
This is my code right now and it is still not working.
If you only apply the
exceptListto 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
includeListto the directories why do you choose to apply theexceptListto them?Do you follow what your own code is doing?
-
@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
exceptListto 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
includeListto the directories why do you choose to apply theexceptListto 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; } -
@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; }@eefesafak Yep, this looks right :)