PDA

View Full Version : How to ensure the creation of a folder?



LuisLoez89
8th September 2015, 10:41
Dear all,
I am having random problems when I try to create a folder. Is there any way to ensure the creation of a folder? I am trying a while loop like the next but i am afraid to create an infinite loop.


//remove dir if exists
QDir previousDir("previousDirPath");
if(previousProprocessingDir.exists())
removeDir(previousDirPath); //Custom process to remove dirs recursively

while (!QDir("previousDirPath").exists()) {
QDir().mkdir("previousDirPath");
}

Thanks

anda_skoa
8th September 2015, 10:54
Well, it depends on why it is failing.

A loop only makes sense if the reason for failing is something that is out side of the control of the program and will happen at some point.

In your situation, what would that be?
Is the removeDir() implemented in an external process and the loop tries to create the directory again and again, succeeding when the removal process finished?

Cheers,
_

LuisLoez89
8th September 2015, 11:01
It looks like the reason of failing is something that is outside of the control of the programm. The errors making the folder are random and ocur rarely but when this happens the execution of my program can't continue because I need the data that should be written in these folders.
The remove dir is not implemented in an external process and rarely is called because the "previousDirPath" should not exists before the execution.

prasad_N
8th September 2015, 11:02
What you want to do exactly ?

If you want to delete existing dir & create new one. after deleting existing dir


if(!QDir("previousDirPath").exists()) {
QDir().mkdir("previousDirPath");
}

even we don't need if check as we already deleted the dir.

LuisLoez89
8th September 2015, 11:48
What i need is to ensure that the folder was created because some times the creation process fails. For that reason I use the while loop but I am afraid to the posibility of generate an infinite loop if the system is not able to create the folder. Is there any way to ensure the creation of a folder?

prasad_N
8th September 2015, 12:17
What i need is to ensure that the folder was created because some times the creation process fails. For that reason I use the while loop but I am afraid to the posibility of generate an infinite loop if the system is not able to create the folder. Is there any way to ensure the creation of a folder?


mkdir & rmdir returns you a bool variables in order to make sure folder creation/deletion succeeded or failed.

anda_skoa
8th September 2015, 12:29
What i need is to ensure that the folder was created because some times the creation process fails.

Then you need to find out why it fails.
Any form of recovery path, be it a loop or something else, needs to at least hav an idea what it is recovering from.

E.g. if the creation fails because the parent directory doesn't exist, a recovery path needs to create the parent first.
If the creation fails because the program doesn't have the necessary filesystem access rights, there is nothing the program itself can do against that.



Is there any way to ensure the creation of a folder?

Not any specific folder, but you should always be able to create a temporary folder.
See QTemporaryDir.

Cheers,
_