Reading subfolders
-
My program is created for reading all .cpp and .h files then count line numbers in a folder. But I want to add one more condition which is check that if there is more subfolders. For example, If I want to read /D folder, the program should read all of the /D folder's .cpp and .h files and subfolders which contains .cpp and .h files. Afterward, all of the files must be seen in the program. Here is my program and codes:
void MainWindow::addItemToList(const QString &file, const int &fileSize, const int &count) { QTableWidgetItem *iFileName = new QTableWidgetItem(file); QTableWidgetItem *iFileSize = new QTableWidgetItem(QString::number(fileSize)); QTableWidgetItem *iFileCount = new QTableWidgetItem(QString::number(count)); int rowCount = ui->tableWidget->rowCount(); ui->tableWidget->insertRow(rowCount); rowCount = ui->tableWidget->rowCount(); ui->tableWidget->setItem(rowCount-1, 0, iFileName); ui->tableWidget->setItem(rowCount-1, 1, iFileSize); ui->tableWidget->setItem(rowCount-1, 2, iFileCount); } QFileInfoList MainWindow::getFileListFromDir(const QString &directory) { QDir qdir(directory); QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h" << "*.hpp" << "*.c" << "*.cpp", QDir::Files); return fileList; } void MainWindow::on_Browse_clicked() { QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if(path.isEmpty()) return; ui->FullPath->setText(path); } void MainWindow::on_Ok_clicked() { QString path = ui->FullPath->text(); if(path.isEmpty()) return; ui->tableWidget->setRowCount(0); QFileInfoList fileList = getFileListFromDir(path); int count = 0; foreach(const QFileInfo& file, fileList) { count = m_ig->funcCountLines(file.filePath()); addItemToList(file.filePath(), file.size(), count); } }
-
QDir::entryInfoList() can also return the subdirectories.
-
@eefesafak said in Reading subfolders:
QFileInfoList fileList = qdir.entryInfoList(QStringList() << "*.h" << "*.hpp" << "*.c" << "*.cpp", QDir::Files);
This only fetches files. Somewhere you need to use similar to fetch directories/folders, and recurse on them finding sub-folders/files.
-
@Christian-Ehrlicher How can i do that by using entryInfoList()? Do you have any suggestions?
-
@eefesafak said in Reading subfolders:
How can i do that by using entryInfoList()?
I don't understand this question. When you find a subdirectory then call entryInfoList on this directory.
-
@eefesafak said in Reading subfolders:
How can i do that by using entryInfoList()
Please read documentation!
Currently you only read files by passing QDir::Files.
You can also read directories: QDir::Files | QDir::AllDirs
QDir::AllDirs ignores the name filter (as mentioned in the documentation), so you will also get all directories. -
@jsulm said in Reading subfolders:
You can also read directories: QDir::Files | QDir::AllDirs
@eefesafak
Probably easiest for you if you useQDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot
.You either pass this on your existing call to get directories, as well as files matching the pattern, and then use
QFileInfo::isDir()
to recognise which returned entries are directories so you can recurse on them, or issue a separate call withQDir::AllDirs | QDir::NoDotAndDotDot
to deal with the sub-directories only. -
@eefesafak said in Reading subfolders:
For example, I want to see "/home/efe/ImageInverter/efe/main.cpp".
That's why you need to reqursevly go into subfolders as @JonB already suggested.
This is a popular programming exercise by the way.