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 copy and rename a Excel file in Qt
Forum Updated to NodeBB v4.3 + New Features

How to copy and rename a Excel file in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 2.7k 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
    rockon209
    wrote on last edited by A Former User
    #1

    Hello guys,
    I want copy and rename a excel file and check it if the rename file is present or not in the folder. If the file is present it should warn and ask to change the name of the file. The code should be robust. I know i can use qfile exist and check it. But i am facing problem after first warning if I again pass the same name it doesnt show that file exist for that i wrote the code in while to so that till the file name is change and does exist it will show the msg but after new name if i want to copy it i can do it. Could anyone please help me with this.
    Thanks

    VRoninV 1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @rockon209
      You need to give more information like OS and Qt for your problem. Also a code section showing some details and the text of the warning you are referring to.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • R rockon209

        Hello guys,
        I want copy and rename a excel file and check it if the rename file is present or not in the folder. If the file is present it should warn and ask to change the name of the file. The code should be robust. I know i can use qfile exist and check it. But i am facing problem after first warning if I again pass the same name it doesnt show that file exist for that i wrote the code in while to so that till the file name is change and does exist it will show the msg but after new name if i want to copy it i can do it. Could anyone please help me with this.
        Thanks

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        @rockon209 said in How to copy and rename a Excel file in Qt:

        but after new name if i want to copy it i can do it

        I didn't get this part, could you explain better?

        
        for(;;){
        QString newFileName = QFileDialog::getSaveFileName(Q_NULLPTR,"Select Destination",QString(),"Excel Files (*.xls *.xlsx)");
        if(newFileName.iseEmpty()){
        // cancel clicked
        break;
        }
        if(QFile::exist(newFileName)){
        // file exists already
        if(QMessageBox::question(Q_NULLPTR,"Overwrite?","The selected file already exist, do you want to overwrite it?")==QMessageBox::Yes){
        if(!QFile::remove(newFileName)){
        // Remove failed, file is open?
        continue;
        }
        }
        else{
        // No selected
        continue;
        }
        }
        if(!QFile::copy(source,newFileName)){
        // copy failed
        continue;
        }
        break;
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • R Offline
          R Offline
          rockon209
          wrote on last edited by rockon209
          #4
          1. I wanted to say I cant copy.

          2. In my case I want to ask the user if the file exist do you want to replace the file. If he says yes then replace the file .If he says no then go back to dialogbox to rename it. If he rename it again the same or other name and that other name also exist it should show the msg again do yo want to replace it. Secondly if file dont exist then copy the file.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Amended the code above to fit your needs

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rockon209
              wrote on last edited by
              #6

              But If I select 'no' i dont want to overwrite, then it should open the dialog box to rename it again how to do that?

              VRoninV 1 Reply Last reply
              0
              • R rockon209

                But If I select 'no' i dont want to overwrite, then it should open the dialog box to rename it again how to do that?

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @rockon209 said in How to copy and rename a Excel file in Qt:

                But If I select 'no' i dont want to overwrite, then it should open the dialog box to rename it again how to do that?

                that's what

                else
                continue;
                

                does. please test the code

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rockon209
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rockon209
                    wrote on last edited by
                    #9

                    Actually i dont knw whts does for(;;) means could you explain

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      for(;;) = while(true) = forever

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1

                      • Login

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