Solved How to Access the Recently created directory to create and save files inside it.
-
QDir newFolder("NewFolder");
if(newFolder.exists()) { qDebug()<<"Folder Exists"; } else { newFolder.mkdir("NewFolder"); //Folder Gets Created Here qDebug()<<"Folder Created"; }
// How do i access the newly created folder and use it to create and save new files inside it
-
This post is deleted! -
Hi, @alanaalison Nice to meet you as well. Thank you for the quick response. I will try it and let you know.
Regards,
Rajesh P
-
Hi, since @alanaalison 's answer is more python-related, I'd like to say something about QDir.
We don't use QDir to create file, we just need the path.
I'm not sure how do you exactly want to save the file.
In most of the cases, when you need to create a file, you pass the path of the file to a IO-related class.
For example, if you want to create a file namednewFile
innewFolder
using QFileQFile file(newFolder.filePath("newFile")); //or newFolder.absoluteFilePath("newFile") if(file.open(QIODevice::WriteOnly)) { //write data to file file.close(); }
-
Hi,
I dont know what @alanaalison s answer is, but I think this wont help you.
You need to set your newly created folder as path for your
QFile
.QDir newFolder; QString filename = "newFile.txt"; if(newFolder.mkdir("NewFolder")) { QFile file(newFolder.path() + filename); // or newFolder.absolutePath() }
-
@Rajesh-Panati
As @Bonnie & @p4ilul say for access the newly created directory --- you just include it in your nextQFile
path.You might also wish to know that you don't need to do the
if (newFolder.exists()) else
test.QFile::mkdir()
(https://doc.qt.io/qt-5/qdir.html#mkdir) is quite happy to be asked to create a directory which does already exist, it won't error it will just do nothing and return false instead of true. So a lot of people will just do thenewFolder.mkdir("NewFolder")
unconditionally. -
@Pl45m4 said in How to Access the Recently created directory to create and save files inside it.:
if(newFolder.mkdir("NewFolder"))
This also includes a check :)
If folder already exists OR creation failed, it will return false, otherwise true.
-
@Pl45m4
Sorry, I had not noticed you had indeed usedif (newFolder.mkdir())
.I had in mind:
// do next line, don't bother to check result // because `false` could mean either directory already exists or cannot create it newFolder.mkdir("NewFolder"); // construct the new file path QFile file(newFolder.path() + filename); // either it works now to open or it does not // I don't care if that is folder does not exist or file in folder does not exist or whatever reason // I'll just report the OS reason in all cases if (!file.open(...)) ...
-
Hi @Bonnie Thank you for your response. I have checked it and i am able to get the path.
Regards,
Rajesh Panati
-
Hi, @Bonnie Thank you for your response. Now i am able to fetch the path of the recently created directory.
Regards,
Rajesh Panati
-
-