help in QDirIterator to a text file
-
@jsulm ok.
but can't figure it out where to add another "| " in code.my head is exploding . please help!!!
-
@saber
Do you know what recursion is? You are trying to walk an arbitrary-depth tree, outputting as you go along according to depth. Writing it recursively will result in less "head explosion" than trying to do it iteratively. -
@saber
Sorry i have no idea how to get depth with QDirIterator and if you dont know what
recursion is, then it will be hard to understand the code anyway.What about just calling linux tree command with QProcess and capture output ?
-
@saber
Sorry i have no idea how to get depth with QDirIterator and if you dont know what
recursion is, then it will be hard to understand the code anyway.What about just calling linux tree command with QProcess and capture output ?
@mrjj
i know that way .but not every distro come with that pre-installed and also i got some problem with qt to capture output .herecan you please review my code?
i just know that i need to add another line if the folder has any item .but can't figure it how in code.
it is important .it will really helpful if it is working. -
@saber
You need a recursive algorithm, no escape from this ...Get ride of QDirIterator::Subdirectories
and call your method for each directory:if(it.fileInfo().isDir()) getFolder(it.filePath()); // recursive call
Extra work is needed for displaying the indentation symbols.
-test if item is the last in this directory (├─ or └─ )
-same test for directory item (add │ or space to the indentation string)
-a way to retreive this indentation string over each recusive call.Not so hard, you can do it ;)
-
@saber
You need a recursive algorithm, no escape from this ...Get ride of QDirIterator::Subdirectories
and call your method for each directory:if(it.fileInfo().isDir()) getFolder(it.filePath()); // recursive call
Extra work is needed for displaying the indentation symbols.
-test if item is the last in this directory (├─ or └─ )
-same test for directory item (add │ or space to the indentation string)
-a way to retreive this indentation string over each recusive call.Not so hard, you can do it ;)
@mpergand said in help in QDirIterator to a text file:
You need a recursive algorithm, no escape from this ...
For the record, one can always write a recursive algorithm iteratively :) But as I suggested earlier OP would be better writing it recursively, as per your outline, to minimize brain-ache!
-
I created a function but don't know why whole app is crashing. Plz help on that...
Code:QString getTree(const QString &path) {
QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks);while (it.hasNext()) {
it.next();
if (it.fileInfo().isDir()) {
qDebug() << it.fileInfo().path();
getTree(it.filePath());
}qDebug() << "\t" << it.filePath();
}
} -
I created a function but don't know why whole app is crashing. Plz help on that...
Code:QString getTree(const QString &path) {
QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks);while (it.hasNext()) {
it.next();
if (it.fileInfo().isDir()) {
qDebug() << it.fileInfo().path();
getTree(it.filePath());
}qDebug() << "\t" << it.filePath();
}
} -
You return nothing, that's the cause.
@JonB
Your're right ofcourse, in fact it doesn"t even compile. -
@Abrar
Never type "your code", always copy & paste! That would save a fair proportion of questions/answers in the forum! -
@Abrar made a function that works .but problems is it takes a long time to make the text file (not enter every folder and get the name).it takes long time if the folder tree is complex.
i wonder how the tree package that @mrjj pointing is take fraction of the time to make that same text file.
here is the code.
int pat = 0; QString strDir; QString getFileFolderTree(const QString &path) { // ├ // └ // │ // ── pat++; QDir dir(path); //dir.setSorting(QDir::DirsFirst); //dir.setNameFilters(QStringList("*")); dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks); QStringList dList = dir.entryList(); for (int i = 0; i < dList.count(); ++i) { QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dList.at(i)); QString strPat = "├── "; for (int i = 1; i < pat; i++) { strPat.insert(0, "│ "); } if (i == dList.count() - 1) { int pos = strPat.count() - 4; strPat = strPat.replace(pos, 1, "└"); } strDir.append(strPat + QDir(dList.at(i)).dirName() + "\n"); getFileFolderTree(newPath); } pat--; dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks); QStringList fList = dir.entryList(); for (int i = 0; i < fList.count(); i++) { QString strPat = "├── "; for (int j = 1; j < pat + 1; j++) { strPat.insert(0, "│ "); } if (i == fList.count() - 1) { strPat = strPat.replace(strPat.count() - 4, 1, "└"); } strDir.append(strPat + fList[i] + "\n"); } // QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System); // while (it.hasNext()) { // it.next(); // if (it.fileInfo().isDir()) { // qDebug() << it.fileInfo().path(); // getFileFolderTree(it.filePath()); // } // qDebug() << "\t" << it.filePath(); // } //qDebug() << strDir; if (!strDir.count()) strDir.append(".\n"); QFile file("/home/abrar/Desktop/UI.txt"); file.open(QIODevice::Text | QIODevice::ReadWrite | QIODevice::Truncate); QTextStream out(&file); out << strDir; file.close(); return QString(); }
so i need some help to dicress the time to make the text file .
help! help! help!
thanks. -
@Abrar made a function that works .but problems is it takes a long time to make the text file (not enter every folder and get the name).it takes long time if the folder tree is complex.
i wonder how the tree package that @mrjj pointing is take fraction of the time to make that same text file.
here is the code.
int pat = 0; QString strDir; QString getFileFolderTree(const QString &path) { // ├ // └ // │ // ── pat++; QDir dir(path); //dir.setSorting(QDir::DirsFirst); //dir.setNameFilters(QStringList("*")); dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks); QStringList dList = dir.entryList(); for (int i = 0; i < dList.count(); ++i) { QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dList.at(i)); QString strPat = "├── "; for (int i = 1; i < pat; i++) { strPat.insert(0, "│ "); } if (i == dList.count() - 1) { int pos = strPat.count() - 4; strPat = strPat.replace(pos, 1, "└"); } strDir.append(strPat + QDir(dList.at(i)).dirName() + "\n"); getFileFolderTree(newPath); } pat--; dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks); QStringList fList = dir.entryList(); for (int i = 0; i < fList.count(); i++) { QString strPat = "├── "; for (int j = 1; j < pat + 1; j++) { strPat.insert(0, "│ "); } if (i == fList.count() - 1) { strPat = strPat.replace(strPat.count() - 4, 1, "└"); } strDir.append(strPat + fList[i] + "\n"); } // QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::System); // while (it.hasNext()) { // it.next(); // if (it.fileInfo().isDir()) { // qDebug() << it.fileInfo().path(); // getFileFolderTree(it.filePath()); // } // qDebug() << "\t" << it.filePath(); // } //qDebug() << strDir; if (!strDir.count()) strDir.append(".\n"); QFile file("/home/abrar/Desktop/UI.txt"); file.open(QIODevice::Text | QIODevice::ReadWrite | QIODevice::Truncate); QTextStream out(&file); out << strDir; file.close(); return QString(); }
so i need some help to dicress the time to make the text file .
help! help! help!
thanks.@saber
It shouldn't take long. But you don't say how many files/directories there are --- if it's thousands, it will take longer....You should put debug statements in or run under debugger to discover just which part is taking the time if you expect anybody to be able to help you. Saying "help! help! help!" doesn't do it.
-
@saber
It shouldn't take long. But you don't say how many files/directories there are --- if it's thousands, it will take longer....You should put debug statements in or run under debugger to discover just which part is taking the time if you expect anybody to be able to help you. Saying "help! help! help!" doesn't do it.
-
@JonB
thanks.
as in my Linux system debuger is not working (i tried to fix), i can't test it.