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 convert a .txt files in a folder to .csv files extension provided by the user
Forum Updated to NodeBB v4.3 + New Features

how to convert a .txt files in a folder to .csv files extension provided by the user

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 303 Views
  • 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.
  • P Offline
    P Offline
    priya a
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • P priya a

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @priya-a Please first use debugger to see what happens when your code is executed.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        priya a
        wrote on last edited by
        #3

        @jsulm i am not getting debugging issue. i am getting the GUI but file extension is not getting changed

        jsulmJ 1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • P priya a

            @jsulm i am not getting debugging issue. i am getting the GUI but file extension is not getting changed

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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