how to convert a .txt files in a folder to .csv files extension provided by the user
-
here is my code
void MainWindow::on_pushButton_browse__clicked()
{
auto folder =QFileDialog::getExistingDirectory(this, "Get Any File");ui->lineEdit_folder_path_->setText(folder);
}
void MainWindow::on_pushButton_start_renaming__clicked()
{auto rename_test = ui->lineEdit_extension_->text(); if(rename_test.isEmpty()) { Log("Extension is empty, Skipping.."); return; } // Get the folder name auto folder = ui->lineEdit_folder_path_->text(); // Get the extension name auto extension = ui->lineEdit_extension_->text(); // Run Operation Rename(folder, extension);
}
void MainWindow::Log(QString msg)
{
ui->plainTextEdit_output_->appendPlainText(msg);
}void MainWindow::on_pushButton_clear__clicked()
{
ui->plainTextEdit_output_->clear();
}void MainWindow::Rename(const QString &folder_name, const QString &extension)
{
if(folder_name.isEmpty())
{
Log("Folder Name is Empty , Skipping..");
return;
}
if(!QFileInfo(folder_name).exists())
{
Log("Folder Name does not Exist : "+ folder_name +" , Skipping..");
return;
}if(extension.isEmpty()) { Log("Extension is Empty , Skipping.."); return; } // Get all the files inside this folder auto list_files = QDir(folder_name).entryInfoList(QDir::Files); // For each file change the name to new name with new extension for(auto const &file_info : list_files) { auto file_path = file_info.filePath(); QString to_rename_path; auto suffix = QFileInfo(file_path).suffix(); if(!suffix.isEmpty()) { to_rename_path = file_path.remove(suffix); to_rename_path.remove("."); } auto new_file_path = to_rename_path + extension; if(QFile::copy(to_rename_path, new_file_path)) { Log("File Rename : Success, Filename - "+file_path+"\n - to - \n"+new_file_path); } else { Log("File Rename : Failed, Filename - "+file_path); } }
}
my older is selected but the files inside it is not getting renamed -
here is my code
void MainWindow::on_pushButton_browse__clicked()
{
auto folder =QFileDialog::getExistingDirectory(this, "Get Any File");ui->lineEdit_folder_path_->setText(folder);
}
void MainWindow::on_pushButton_start_renaming__clicked()
{auto rename_test = ui->lineEdit_extension_->text(); if(rename_test.isEmpty()) { Log("Extension is empty, Skipping.."); return; } // Get the folder name auto folder = ui->lineEdit_folder_path_->text(); // Get the extension name auto extension = ui->lineEdit_extension_->text(); // Run Operation Rename(folder, extension);
}
void MainWindow::Log(QString msg)
{
ui->plainTextEdit_output_->appendPlainText(msg);
}void MainWindow::on_pushButton_clear__clicked()
{
ui->plainTextEdit_output_->clear();
}void MainWindow::Rename(const QString &folder_name, const QString &extension)
{
if(folder_name.isEmpty())
{
Log("Folder Name is Empty , Skipping..");
return;
}
if(!QFileInfo(folder_name).exists())
{
Log("Folder Name does not Exist : "+ folder_name +" , Skipping..");
return;
}if(extension.isEmpty()) { Log("Extension is Empty , Skipping.."); return; } // Get all the files inside this folder auto list_files = QDir(folder_name).entryInfoList(QDir::Files); // For each file change the name to new name with new extension for(auto const &file_info : list_files) { auto file_path = file_info.filePath(); QString to_rename_path; auto suffix = QFileInfo(file_path).suffix(); if(!suffix.isEmpty()) { to_rename_path = file_path.remove(suffix); to_rename_path.remove("."); } auto new_file_path = to_rename_path + extension; if(QFile::copy(to_rename_path, new_file_path)) { Log("File Rename : Success, Filename - "+file_path+"\n - to - \n"+new_file_path); } else { Log("File Rename : Failed, Filename - "+file_path); } }
}
my older is selected but the files inside it is not getting renamed -
@jsulm i am not getting debugging issue. i am getting the GUI but file extension is not getting changed
@priya-a I didn't say you have a "debugging issue". I said you should run your app through debugger to see what your code does. This is first thing to do to find out what the problem is...
Also, does list_files contain anything? What does QFile::copy return? And why do you remove suffix from the file you want to rename? How should renaming work then? QFile::copy will not find the file to rename as you changed its name...