[Solved]How to list all files on linux correctly
-
I want to get all files on linux, and i'm trying this code:
@
void MainWindow::countstart()
{
QFileInfoList drives = QDir::drives();
foreach (QFileInfo drive, drives)
{
count(drive.absoluteFilePath());
}
}
int total;
void MainWindow::count(QString path)
{
QDir dir(path);
QFileInfoList filelist = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QFileInfo fileinfo, filelist)
{
if (fileinfo.isDir())
{
count(fileinfo.absoluteFilePath());
}
if (fileinfo.isFile())
{
total++;
}
}
}
@but after 100.000.000 files listed it don't stop and the system has only 300.000 files,
how can i list files correctly?
Note: On Windows the code works correctly -
Since you don't tell us what listing() is supposed to do, or how you have attempted to do it, we have no way to know what you are doing wrong.
-
I posted the full code, what i'm doing wrong?
-
Recursive function? - you are doing it wrong! :)
You have forgot about symlinks, they can be recursive too. like symlink local in /usr pointing to ../ ;) or /usr/X11R6 linking back to /usr
Also, int as an file counter is a bit to small, isn't it?
And if you are doing an recursion, do it in the right way and don't forget about memory usage(it will increase speed too)
working sample:
@
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>long long countFiles(const QDir & dir)
{
long long counter = 0;
const QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach(const QFileInfo & info, list)
{
if(info.isFile() && !info.isSymLink())counter++;
else if(info.isDir() && !info.isSymLink())counter += countFiles(info.absoluteFilePath());
}
return counter;
}int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir dir("/");
qDebug() << "Files on system: " << countFiles(dir);
return a.exec();
}@
-
Thanks, it worked!