Solved How do you check a file exits in another file ?
-
I know you guys will have a lot of doubts regarding to the tile of this topic. Dont worry! I'll give a further explanation of my title. OK, lets start with this scenario, you have a folder A, inside the folder you ll have another three folders, the name of these three folder inside folder A are B,C and D. Now, Folder B and D is a empty folder, which means the folders B and D dont have anything inside the folder, but for folder C ,yes, its not empty, there is a .txt file inside folder C. Right now, i want to check the .txt. file in in which folder , want to check either is in B,C or D. Let me make my question simpler, how do i check which files have the .txt file in folder A?
#include "checkforfiles_form.h" #include "ui_checkforfiles_form.h" #include <QFileInfo> #include <qdebug.h> using namespace std; CheckForFiles_Form::CheckForFiles_Form(QWidget *parent) : QMainWindow(parent), ui(new Ui::CheckForFiles_Form) { ui->setupUi(this); } CheckForFiles_Form::~CheckForFiles_Form() { delete ui; } void CheckForFiles_Form::on_pushButton_clicked() { QFileInfo fileInfo; fileInfo.setFile("C:\\Users\\USER\\Desktop\\folderA"); qDebug()<<fileInfo.exists(); }
Above codes only will tell us folder A is really exist in that directory . But this is not i want , i want to search through the Folder A go inside the folder A, and check for which files in folder A containing .txt file?is it B,C or D? Some one here please give me some knowledge ,tips, reference or live example to proceed my task . Thank you
-
QStringList QDir::entryList(Filters filters = NoFilter, sort = NoSort)
may be what you are looking for.
For example (if i understood your scenario right)
QDir folderA("C:\\Users\\USER\\Desktop\\folderA"); QStringList foldersinA = folderA.entryList(QDir::Dirs); foreach(QString folder,foldersinA) { QDir f(folder); QStringList txtlist = f.entryList(QStringList() << "*.txt",QDir::Files); //do something with the filenames in txtlist }
-
Hi,
Since you want to check several folder, you can use QDirIterator in addition to what @the_ suggested.
On a side note, since your a using Qt, you should use the unix notation for path (i.e.
"C:/Users/USER/Desktop/"
) that will avoid you headaches if your forget to escape your backslashes. -
Dear All, this is the solution for this specific problem . Thank you
void CheckForFiles_Form ::recursiveAddDir(QDir d, bool recursive) { d.setSorting(QDir::Name); QDir::Filters df = QDir::Files | QDir::NoDotAndDotDot; if(recursive) df |= QDir::Dirs; QStringList qsl = d.entryList (df,QDir::Name|QDir::DirsFirst); foreach (const QString &entry, qsl){ QFileInfo fInfo(d,entry); if(fInfo.isDir()){ QDir sd(fInfo.absoluteFilePath()); recursiveAddDir(sd,true); }else{ if(fInfo.completeSuffix()== "txt") qDebug() << fInfo.filePath(); } } }
void CheckForFiles_Form::on_pushButton_clicked() { QDir r("C:/Users/USER/Desktop/FileA/"); recursiveAddDir(r, true); }
-
Why not use QDirIterator ? You'd only have to do the file listing and check.