QProcess and cmd.exe not working when path with with accented characters
-
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? -
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?@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.
-
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?@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.
-
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 screenQString 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_OBJECTpublic:
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 = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui
/>
-
@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.
-
@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?
@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
You are sending input to the running
cmd.exe
encoded as UTF-8 but reading the output fromcmd.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().
-
@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
You are sending input to the running
cmd.exe
encoded as UTF-8 but reading the output fromcmd.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().
@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()).
-
@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
You are sending input to the running
cmd.exe
encoded as UTF-8 but reading the output fromcmd.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().
@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...