@@ -307,18 +307,21 @@ void MainWindow::run_copy_thread(MainWindow *w) {
307307 while (!w->copy_queue .isEmpty ()) {
308308 file = w->copy_queue .dequeue ();
309309
310+ QFileInfo new_file = QFileInfo (file.second );
310311
311- if (QFile (file.second ).exists ()) {
312- qDebug (" Destination already exists, renaming" );
313- int i = 1 ;
314- while (QFile (file.second + " _" + i).exists ()) { i++; }
312+ if (new_file.exists ()) {
313+ qDebug (" File exists, renaming..." );
314+ QString new_file_name = QDir (new_file.absolutePath ()).filePath (new_file.baseName ());
315315
316- qDebug ( " Copying %s to %s " , file. first . toStdString (). c_str (), (file. second + " _ " + i). toStdString (). c_str ()) ;
317- QFile::copy (file. first , file. second + " _ " + i);
318- } else {
319- qDebug ( " Copying %s to %s " , file. first . toStdString (). c_str (), file. second . toStdString (). c_str ()) ;
320- QFile::copy (file. first , file. second );
316+ int i = 1 ;
317+ while (new_file. exists ()) {
318+ new_file = QFileInfo ( QString ( " %1_%2.%3 " ). arg (new_file_name, QString::number (i), new_file. suffix ()));
319+ i++ ;
320+ }
321321 }
322+
323+ qDebug (" Copying %s to %s" , file.first .toStdString ().c_str (), new_file.filePath ().toStdString ().c_str ());
324+ QFile::copy (file.first , new_file.filePath ());
322325 }
323326 w->ui ->button_go ->setEnabled (true );
324327}
0 commit comments