How to copy and rename a Excel file in Qt



  • 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


  • Moderators

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



  • @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;
    }
    


    1. I wanted to say I cant copy.
    1. 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.


  • Amended the code above to fit your needs



  • 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?



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



  • This post is deleted!


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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.