QT Creator - QDir
-
Hi. I am trying to use QDir to delete files and folders inside a folder. I want it to delete all the folders and files except one folder and it's files. Is there any way I can do this?
Many thanks
Hassan -
Hi
you have to recursively run through all and skip the folder you want to keep.bool removeDir(const QString & dirName) { bool result = true; QDir dir(dirName); if (dir.exists(dirName)) { Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { if (info.isDir() && yourcheck ) { result = removeDir(info.absoluteFilePath()); } else { result = QFile::remove(info.absoluteFilePath()); } if (!result) { return result; } } result = dir.rmdir(dirName); } return result; }
just need to add a check its its the folder you want to keep.
https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt -
Hi
you have to recursively run through all and skip the folder you want to keep.bool removeDir(const QString & dirName) { bool result = true; QDir dir(dirName); if (dir.exists(dirName)) { Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { if (info.isDir() && yourcheck ) { result = removeDir(info.absoluteFilePath()); } else { result = QFile::remove(info.absoluteFilePath()); } if (!result) { return result; } } result = dir.rmdir(dirName); } return result; }
just need to add a check its its the folder you want to keep.
https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt@mrjj said in QT Creator - QDir:
Hi
you have to recursively run through all and skip the folder you want to keep.bool removeDir(const QString & dirName) { bool result = true; QDir dir(dirName); if (dir.exists(dirName)) { Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { if (info.isDir() && yourcheck ) { result = removeDir(info.absoluteFilePath()); } else { result = QFile::remove(info.absoluteFilePath()); } if (!result) { return result; } } result = dir.rmdir(dirName); } return result; }
just need to add a check its its the folder you want to keep.
https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qtWould be easier for me to just write the paths and delete needed for a batch file delete or implement this.
My context is, I'm using Q String to store and save a path to a text file. I'd then like to read this line from the text file and add the rest of the path to it (folders I want to delete). The problem I am having with the batch file is that the path needs to be surrounded in "" in the batch file but with Q TextStream I can't do this. -
@mrjj said in QT Creator - QDir:
Hi
you have to recursively run through all and skip the folder you want to keep.bool removeDir(const QString & dirName) { bool result = true; QDir dir(dirName); if (dir.exists(dirName)) { Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { if (info.isDir() && yourcheck ) { result = removeDir(info.absoluteFilePath()); } else { result = QFile::remove(info.absoluteFilePath()); } if (!result) { return result; } } result = dir.rmdir(dirName); } return result; }
just need to add a check its its the folder you want to keep.
https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qtWould be easier for me to just write the paths and delete needed for a batch file delete or implement this.
My context is, I'm using Q String to store and save a path to a text file. I'd then like to read this line from the text file and add the rest of the path to it (folders I want to delete). The problem I am having with the batch file is that the path needs to be surrounded in "" in the batch file but with Q TextStream I can't do this.@FreeHassan29
ok. im not sure i think bat file is easier.
Anyway, why cant you write " in TextStream ?
Do you forget to escape the " ? -
@FreeHassan29
ok. im not sure i think bat file is easier.
Anyway, why cant you write " in TextStream ?
Do you forget to escape the " ?@mrjj To be honest. I don't know how to escape it. This is what I need.
FMC_dir = C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache
"rmdir "+ FMC_dir + "/priv /q /s "
I need text written to the bat to be like this
"C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache\priv"
In quotes or the bat doesn't work.
Thanks -
@mrjj To be honest. I don't know how to escape it. This is what I need.
FMC_dir = C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache
"rmdir "+ FMC_dir + "/priv /q /s "
I need text written to the bat to be like this
"C:\Users\Hassan\AppData\Local\FiveM\FiveM.app\cache\priv"
In quotes or the bat doesn't work.
Thankswell its just
\" something like stream << "rmdir " << "\"" << FMC_dir << "/priv" << "\"" << "/q /s "; or QString quote = "\""; "rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s "
-
well its just
\" something like stream << "rmdir " << "\"" << FMC_dir << "/priv" << "\"" << "/q /s "; or QString quote = "\""; "rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s "
@mrjj That works but I forgot to add, the /priv is a part of the path. Would I do this?
ccache << "rmdir " << """ << FMC_dir << """ << "priv /q /s " << "\n"; -
@mrjj That works but I forgot to add, the /priv is a part of the path. Would I do this?
ccache << "rmdir " << """ << FMC_dir << """ << "priv /q /s " << "\n";@FreeHassan29
just saw that."rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s " stream << "rmdir " << "\"" << FMC_dir << "/priv" << "\"" << "/q /s ";
-
@FreeHassan29
just saw that."rmdir "+ quote +FMC_dir + "/priv"+quote + "/q /s " stream << "rmdir " << "\"" << FMC_dir << "/priv" << "\"" << "/q /s ";
@mrjj That's perfect. Thank you very much. Last question while I have your attention, would I use QProcess to launch this bat, and would QProcess work for creating another button to launch an application and connect to an IP?
-
@mrjj That's perfect. Thank you very much. Last question while I have your attention, would I use QProcess to launch this bat, and would QProcess work for creating another button to launch an application and connect to an IP?
@FreeHassan29
Yes you can use QProcess to launch a bat file but you must use the /C
parameterprocess.setProgram( "cmd.exe" ); // we need this to run the bat process.setArguments( { "/C", " E:\\deleteme.bat" ); // notice the /C
Notice sample uses startDetached. you dont need that.
https://stackoverflow.com/questions/51740460/execute-batch-file-with-qt-as-a-new-processand yes, you can simply use QProcess to launch other app. (cmd.exe and /C not needed for that)
-
@FreeHassan29
Yes you can use QProcess to launch a bat file but you must use the /C
parameterprocess.setProgram( "cmd.exe" ); // we need this to run the bat process.setArguments( { "/C", " E:\\deleteme.bat" ); // notice the /C
Notice sample uses startDetached. you dont need that.
https://stackoverflow.com/questions/51740460/execute-batch-file-with-qt-as-a-new-processand yes, you can simply use QProcess to launch other app. (cmd.exe and /C not needed for that)
@mrjj
Thank you very much. If I didn't specify a directory because I wanted the bat to be in the same folder as the bat file what would I do. I tried it but it doesn't seem to be executing the bat.
QProcess process;
process.setProgram( "cmd.exe" ); // we need this to run the bat
process.setArguments( { "/C", "ClearCache.bat"} ); // notice the /C
Stack overflow is down right now -
@mrjj
Thank you very much. If I didn't specify a directory because I wanted the bat to be in the same folder as the bat file what would I do. I tried it but it doesn't seem to be executing the bat.
QProcess process;
process.setProgram( "cmd.exe" ); // we need this to run the bat
process.setArguments( { "/C", "ClearCache.bat"} ); // notice the /C
Stack overflow is down right now@FreeHassan29
Hi
you have to use full path to the bat file or else it can find it.
When your programs runs, the current folder is most likely inside the build folder.
You can use
qApp->applicationDirPath(); to say "where .exe" is
and place bat file there.
also the code you shown is not complete
need to call start or startDetached.sample from SO
QProcess process; process.setProgram( "cmd.exe" ); process.setArguments( { "/C", R"(E:\deleteme.bat)" } ); process.setWorkingDirectory( R"(E:\)" ); process.setStandardOutputFile( QProcess::nullDevice() ); process.setStandardErrorFile( QProcess::nullDevice() ); process.startDetached();
-
@FreeHassan29
Hi
you have to use full path to the bat file or else it can find it.
When your programs runs, the current folder is most likely inside the build folder.
You can use
qApp->applicationDirPath(); to say "where .exe" is
and place bat file there.
also the code you shown is not complete
need to call start or startDetached.sample from SO
QProcess process; process.setProgram( "cmd.exe" ); process.setArguments( { "/C", R"(E:\deleteme.bat)" } ); process.setWorkingDirectory( R"(E:\)" ); process.setStandardOutputFile( QProcess::nullDevice() ); process.setStandardErrorFile( QProcess::nullDevice() ); process.startDetached();
@mrjj It worked without me having to set the path. I just used the filename like this.
QProcess process;
process.setProgram( "cmd.exe" ); // we need this to run the bat
process.setArguments( { "/C", "ClearCache.bat"} ); // notice the /C
process.startDetached();It only ran because I used start detached
-
Ok super. :)