Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Access the Recently created directory to create and save files inside it.
Qt 6.11 is out! See what's new in the release blog

How to Access the Recently created directory to create and save files inside it.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajesh Panati
    wrote on last edited by
    #1

    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

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • A Offline
      A Offline
      alanaalison
      Banned
      wrote on last edited by
      #2
      This post is deleted!
      R 1 Reply Last reply
      -1
      • A alanaalison

        This post is deleted!

        R Offline
        R Offline
        Rajesh Panati
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          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 named newFile in newFolder using QFile

          QFile file(newFolder.filePath("newFile"));  //or newFolder.absoluteFilePath("newFile")
          if(file.open(QIODevice::WriteOnly)) {
            //write data to file
            file.close();
          }
          
          R 2 Replies Last reply
          2
          • R Rajesh Panati

            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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @Rajesh-Panati

            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()
                 }
            
            

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            R 1 Reply Last reply
            1
            • R Rajesh Panati

              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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Rajesh-Panati
              As @Bonnie & @p4ilul say for access the newly created directory --- you just include it in your next QFile 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 the newFolder.mkdir("NewFolder") unconditionally.

              Pl45m4P 1 Reply Last reply
              1
              • JonBJ JonB

                @Rajesh-Panati
                As @Bonnie & @p4ilul say for access the newly created directory --- you just include it in your next QFile 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 the newFolder.mkdir("NewFolder") unconditionally.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @JonB

                @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.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                JonBJ 1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @JonB

                  @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.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Pl45m4
                  Sorry, I had not noticed you had indeed used if (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(...))
                      ...
                  
                  R 1 Reply Last reply
                  1
                  • B Bonnie

                    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 named newFile in newFolder using QFile

                    QFile file(newFolder.filePath("newFile"));  //or newFolder.absoluteFilePath("newFile")
                    if(file.open(QIODevice::WriteOnly)) {
                      //write data to file
                      file.close();
                    }
                    
                    R Offline
                    R Offline
                    Rajesh Panati
                    wrote on last edited by
                    #9

                    Hi @Bonnie Thank you for your response. I have checked it and i am able to get the path.

                    Regards,

                    Rajesh Panati

                    1 Reply Last reply
                    0
                    • B Bonnie

                      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 named newFile in newFolder using QFile

                      QFile file(newFolder.filePath("newFile"));  //or newFolder.absoluteFilePath("newFile")
                      if(file.open(QIODevice::WriteOnly)) {
                        //write data to file
                        file.close();
                      }
                      
                      R Offline
                      R Offline
                      Rajesh Panati
                      wrote on last edited by
                      #10

                      Hi, @Bonnie Thank you for your response. Now i am able to fetch the path of the recently created directory.

                      Regards,

                      Rajesh Panati

                      1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4

                        @Rajesh-Panati

                        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()
                             }
                        
                        
                        R Offline
                        R Offline
                        Rajesh Panati
                        wrote on last edited by
                        #11

                        Hi @Pl45m4 , Thank you for your response.

                        Regards,

                        Rajesh Panati

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Pl45m4
                          Sorry, I had not noticed you had indeed used if (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(...))
                              ...
                          
                          R Offline
                          R Offline
                          Rajesh Panati
                          wrote on last edited by
                          #12

                          Hi @JonB, Thank you for the response.

                          Regards,

                          Rajesh P

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved