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. QProcess and cmd.exe not working when path with with accented characters
Forum Update on Monday, May 27th 2025

QProcess and cmd.exe not working when path with with accented characters

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 395 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.
  • V Offline
    V Offline
    Vec44
    wrote on last edited by
    #1

    Re : [Solved] Running cmd.exe in QProcess without showing Command Prompt window?

    I tried to copy files from deep folder ( longpath > 259) using QFile::copy(source, dest) but it does not work: I got ’file not fond’.

    NB in W10, longpath is enabled.

    If I do the same in a Windows console with ’copy source dest /Y’ files are found and copied.

    So I try using QProcess and cmd.exe as described by topic/29517.
    That works for paths with non accented characters like ’Deplaces’, but doesn’t with accented characters like ’Déplacés’.

    In lineEdit, If I type ’copy [source] folder1\ folder2......\Déplacés\OneFile /Y, In textEdit, instead of that I see copy [source] folder1\ folder2......\DÙplacÙs\ which could explain that file isn’t fond...

    When using ’process->write(ptr)’ the text of the command is changed and I get the error.
    How could I overcome this?

    Christian EhrlicherC C 2 Replies Last reply
    0
    • V Vec44

      Re : [Solved] Running cmd.exe in QProcess without showing Command Prompt window?

      I tried to copy files from deep folder ( longpath > 259) using QFile::copy(source, dest) but it does not work: I got ’file not fond’.

      NB in W10, longpath is enabled.

      If I do the same in a Windows console with ’copy source dest /Y’ files are found and copied.

      So I try using QProcess and cmd.exe as described by topic/29517.
      That works for paths with non accented characters like ’Deplaces’, but doesn’t with accented characters like ’Déplacés’.

      In lineEdit, If I type ’copy [source] folder1\ folder2......\Déplacés\OneFile /Y, In textEdit, instead of that I see copy [source] folder1\ folder2......\DÙplacÙs\ which could explain that file isn’t fond...

      When using ’process->write(ptr)’ the text of the command is changed and I get the error.
      How could I overcome this?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Vec44 said in QProcess and cmd.exe not working when path with with accented characters:

      How could I overcome this?

      First by providing a minimal, compilable example on what you're doing.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • V Vec44

        Re : [Solved] Running cmd.exe in QProcess without showing Command Prompt window?

        I tried to copy files from deep folder ( longpath > 259) using QFile::copy(source, dest) but it does not work: I got ’file not fond’.

        NB in W10, longpath is enabled.

        If I do the same in a Windows console with ’copy source dest /Y’ files are found and copied.

        So I try using QProcess and cmd.exe as described by topic/29517.
        That works for paths with non accented characters like ’Deplaces’, but doesn’t with accented characters like ’Déplacés’.

        In lineEdit, If I type ’copy [source] folder1\ folder2......\Déplacés\OneFile /Y, In textEdit, instead of that I see copy [source] folder1\ folder2......\DÙplacÙs\ which could explain that file isn’t fond...

        When using ’process->write(ptr)’ the text of the command is changed and I get the error.
        How could I overcome this?

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #3

        @Vec44 said in QProcess and cmd.exe not working when path with with accented characters:

        In lineEdit, If I type ’copy [source] folder1\ folder2......\Déplacés\OneFile /Y, In textEdit, instead of that I see copy [source] folder1\ folder2......\DÙplacÙs\ which could explain that file isn’t fond...

        Failing to handle character encoding correctly would be my guess.

        V 1 Reply Last reply
        0
        • V Offline
          V Offline
          Vec44
          wrote on last edited by
          #4

          Hereafter the code used:

          <
          //MainWindow.cpp
          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QTextCodec>
          #include <QtDebug>

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

          //Increase size of text on screen
          QFont font(this->font());
          font.setPointSizeF(9.5);//tout en bloc -> texte, status, boutons, comboBox
          QApplication::setFont(font);
          
          process = new QProcess(this);
          connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(on_StdoutAvailable()) );
          connect(ui->lineEdit, SIGNAL(returnPressed()),
                  this, SLOT(on_lineEdit_returnPressed()));
          process->start("cmd.exe");
          

          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::on_lineEdit_returnPressed()
          {
          if (process->isOpen())
          {
          QByteArray ba(ui->lineEdit->text().toUtf8() + '\n') ;
          char *ptr = ba.data();
          int numBytes = process->write(ptr);

              process->write(ptr);
          
              //Check
          	while (*ptr) {
                  qDebug()<< "[" << *ptr << "]";
                  ++ptr;
              }
          
              ui->lineEdit->clear();
          }
          

          }
          void MainWindow::on_StdoutAvailable()
          {
          QByteArray ba = process->readAllStandardOutput();
          QTextCodec* codec = QTextCodec::codecForName("IBM-850");//decode accented characters on screen

          QString chaineBrute = codec->toUnicode(ba);//QTextEdit use Unicode for QString
          ui->plainTextEdit->appendPlainText(chaineBrute);//show ocnverted string
          

          }

          //Mainwindow.h
          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H

          #include <QMainWindow>
          #include <QProcess>

          namespace Ui {
          class MainWindow;
          }

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

          public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();

          public slots:
          void on_lineEdit_returnPressed();
          void on_StdoutAvailable();

          private:
          Ui::MainWindow *ui;
          QProcess *process;
          };

          #endif // MAINWINDOW_H

          /Command.pro
          #-------------------------------------------------

          Project created by QtCreator 2023-04-08T09:58:35

          #-------------------------------------------------

          QT += core gui

          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

          TARGET = Command
          TEMPLATE = app

          SOURCES += main.cpp
          mainwindow.cpp

          HEADERS += mainwindow.h

          FORMS += mainwindow.ui

          />

          1 Reply Last reply
          0
          • C ChrisW67

            @Vec44 said in QProcess and cmd.exe not working when path with with accented characters:

            In lineEdit, If I type ’copy [source] folder1\ folder2......\Déplacés\OneFile /Y, In textEdit, instead of that I see copy [source] folder1\ folder2......\DÙplacÙs\ which could explain that file isn’t fond...

            Failing to handle character encoding correctly would be my guess.

            V Offline
            V Offline
            Vec44
            wrote on last edited by
            #5

            @ChrisW67
            Yes, I think so but unfortunately, all codecs I use fail (may be the way I code...).

            Any idea about the way of coding text entered in LineEdit to be conform with one expected by cmd copy?

            C 1 Reply Last reply
            0
            • V Vec44

              @ChrisW67
              Yes, I think so but unfortunately, all codecs I use fail (may be the way I code...).

              Any idea about the way of coding text entered in LineEdit to be conform with one expected by cmd copy?

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #6

              @Vec44 Could you please edit your post and place the code in a code block (or blocks). That is the </> button above, or use three backticks ``` on a line before and after the code.

              You are sending input to the running cmd.exe encoded as UTF-8 but reading the output from cmd.exe as CP-850. Try QString::toLocal8Bit() instead of QString::toUtf8(), or explicitly convert to CP-850. If your Windows has been configured to use UTF-8 rather than an 8-bit code page then you would adjust the reading conversion.

              You should also look at QFile for information about encoding/decoding paths. You might try this with the static QFile::copy().

              V 2 Replies Last reply
              1
              • C ChrisW67

                @Vec44 Could you please edit your post and place the code in a code block (or blocks). That is the </> button above, or use three backticks ``` on a line before and after the code.

                You are sending input to the running cmd.exe encoded as UTF-8 but reading the output from cmd.exe as CP-850. Try QString::toLocal8Bit() instead of QString::toUtf8(), or explicitly convert to CP-850. If your Windows has been configured to use UTF-8 rather than an 8-bit code page then you would adjust the reading conversion.

                You should also look at QFile for information about encoding/decoding paths. You might try this with the static QFile::copy().

                V Offline
                V Offline
                Vec44
                wrote on last edited by
                #7

                @ChrisW67 Of course, I first try with < QFile:copy(source,dest) /> but it works <ith accented characters when short paths, when longpaths very time I got 'File not found'; it's the reason I think of use cmd.exe and copy as I tried whith success from windows terminal...

                If I could overcome with codec it would be OK.

                NB: In fact, instead of using lineEdit, I plane to pick source path from a text file (list of all the files I can't access for QFile:copy()).

                1 Reply Last reply
                0
                • C ChrisW67

                  @Vec44 Could you please edit your post and place the code in a code block (or blocks). That is the </> button above, or use three backticks ``` on a line before and after the code.

                  You are sending input to the running cmd.exe encoded as UTF-8 but reading the output from cmd.exe as CP-850. Try QString::toLocal8Bit() instead of QString::toUtf8(), or explicitly convert to CP-850. If your Windows has been configured to use UTF-8 rather than an 8-bit code page then you would adjust the reading conversion.

                  You should also look at QFile for information about encoding/decoding paths. You might try this with the static QFile::copy().

                  V Offline
                  V Offline
                  Vec44
                  wrote on last edited by
                  #8

                  @ChrisW67 With QFile:copy() I'm unable to access deeppath as I mentioned previously and that is the reason why I use cmd.exe.

                  So I returned to my own code and changed the text picked up using lineEdit :
                  < void MainWindow::on_lineEdit_returnPressed()
                  {
                  if (process->isOpen())
                  {
                  QString s = ui->lineEdit->text() + "\n";
                  QTextCodec* codec = QTextCodec::codecForName("IBM-850");
                  QByteArray ba = codec->fromUnicode(s);
                  }
                  />

                  And now all is working correctly!
                  'I tried first with dir ["longpath with accented characters"] and it works...

                  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