Results 1 to 7 of 7

Thread: How to ensure the creation of a folder?

  1. #1
    Join Date
    Apr 2015
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to ensure the creation of a folder?

    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.

    Qt Code:
    1. //remove dir if exists
    2. QDir previousDir("previousDirPath");
    3. if(previousProprocessingDir.exists())
    4. removeDir(previousDirPath); //Custom process to remove dirs recursively
    5.  
    6. while (!QDir("previousDirPath").exists()) {
    7. QDir().mkdir("previousDirPath");
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to ensure the creation of a folder?

    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,
    _

  3. #3
    Join Date
    Apr 2015
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to ensure the creation of a folder?

    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.

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to ensure the creation of a folder?

    What you want to do exactly ?

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

    Qt Code:
    1. if(!QDir("previousDirPath").exists()) {
    2. QDir().mkdir("previousDirPath");
    3. }
    To copy to clipboard, switch view to plain text mode 

    even we don't need if check as we already deleted the dir.
    Thanks :-)

  5. #5
    Join Date
    Apr 2015
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to ensure the creation of a folder?

    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?

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to ensure the creation of a folder?

    Quote Originally Posted by LuisLoez89 View Post
    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.
    Thanks :-)

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to ensure the creation of a folder?

    Quote Originally Posted by LuisLoez89 View Post
    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.

    Quote Originally Posted by LuisLoez89 View Post
    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,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    LuisLoez89 (8th September 2015)

Similar Threads

  1. Qt 5.1 : Want to put qt.conf file in different folder that application exe folder
    By Rajesh.Rathod in forum Installation and Deployment
    Replies: 1
    Last Post: 20th July 2014, 14:48
  2. folder creation using the dialog box .
    By riarioriu3 in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2012, 15:17
  3. Replies: 0
    Last Post: 13th October 2010, 04:27
  4. QfileDialog new folder creation event
    By vjsharma_30 in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2010, 16:04
  5. Replies: 1
    Last Post: 19th January 2010, 21:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.