Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use QProcess with setArguments() and folders with double word name?



  • Hello,

    Let there is a folder name "folder name" and the application abc.exe there is in:
    C:\Users\tom\def\folder name\pqr\abc.exe

    I would like to run this abc.exe from QProcess:

    1. When I start that application with QProcess, QT add quotes to path like this:
      "C:\Users\tom\def\folder name\pqr\abc.exe" ?

    2. For example my abc.exe have 2 arguments:
      arg1 - single word
      arg name - double word

    And I would like to run application like this: cmd.exe /c ""C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name""

    How can I do this? Like this? Or is there a better solution?

    proc->setProgram("cmd.exe");
    proc->setArguments(QStringList()<<"/c"<<R"("C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name")");
    

  • Lifetime Qt Champion

    Hi,

    My gut filling goes with escaping the double quotes in the string you put in the parameter list.



  • @SGaist Thank you for answer. But why? I use raw string


  • Lifetime Qt Champion

    @TomNow99
    cmd.exe has a hard time with folder names with spaces still.


  • Lifetime Qt Champion

    @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

    @SGaist Thank you for answer. But why? I use raw string

    But you are not building the list correctly. Each element must be separately put in the string list.



  • @SGaist Yeah, but In the second example ( with arguments ) I have something like argument in argument :D So I don't know how to do this

    @mrjj Thank you for answer. I know that, but now I have to do this with cmd


  • Lifetime Qt Champion

    @TomNow99
    Hi string list is clever so you can just have 2 of them

    QString program = "cmd.exe";
    QStringList arguments;
    QStringList arguments2;
    arguments2 << "1" << "2";
    arguments << "C:\Users\tom\def\folder name\pqr\abc.exe" << arguments2 << "whatever" << "and more" ;
    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);



  • @mrjj Hmmm. Thank you.

    Why do you have 2 QStringLists?

    Can I do:

    arguments << "C:\Users\tom\def\folder name\pqr\abc.exe" <<"1"<<"2"<< "whatever" << "and more" ;
    

    ?


  • Lifetime Qt Champion

    @TomNow99
    Yes also like that :)



  • @mrjj Perfect. In cmd Can I do:

    cmd.exe /c "C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name" ?

    I don't like cmd.exe /c ""path" arg1 arg2"

    Yes, I know that execute cmd in cmd is strange option, but sometimes I need that :)


  • Lifetime Qt Champion

    @TomNow99
    All arguments goes in argument list as else it might not work.
    and /c is an argument so nope it might not work with .exe part.



  • @TomNow99
    Why are you wanting to run this command via cmd /c, which just makes the quoting an issue?

    Furthermore, if you insist on doing that, I would have a very careful read of the output from cmd /? where it discusses its quoting rules in this case.



  • @JonB @SGaist @mrjj
    Of course I can change cmd command and don't use "cmd /c", but I would like to know how to add Arguments, when some application need it in the form:

    app.exe ""arg 1" "arg 2" "arg 3""

    arg 1, arg 2 and arg 3 are double words.

    Now I have 2 apps:

    The first one (C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe) which create file:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        if(argc==3)
        {
            QFile file(argv[1]);
            file.open(QIODevice::WriteOnly);
            file.write(argv[2]);
            file.close();
        }
        
        exit(0);
        return a.exec();
    }
    

    And the second one which execute the first one:

        proc = new QProcess;
        proc->setProgram(R"(cmd.exe)");
        QStringList list;
        list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
    
        proc->setArguments(QStringList()<<"/C"<<list);
        proc->start();
    

    But it doesn't work ( no create file ).



  • @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

    I give up!

    Of course I can change cmd command and don't use "cmd /c", but I would like to know how to add Arguments, when some application need it in the form:
    app.exe ""arg 1" "arg 2" "arg 3""

    So your app takes quoted arguments. That's fine. But what does this have to do with wanting to run it via cmd /c??

    proc->setArguments(QStringList()<<"/C"<<list);
    

    I don't know that's right, to pass a list as a single element in the new list.

    But it doesn't work ( no create file ).

    For goodness sake, make the first program print out whatever it receives as its arguments, so that neither you nor we have to guess what it's getting! This is just standard development debugging. It is possible it does not receive the arguments you think it does....



  • @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

    proc = new QProcess;
    proc->setProgram(R"(cmd.exe)");
    QStringList list;
    list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
    
    proc->setArguments(QStringList()<<"/C"<<list);
    proc->start();
    

    This approach most likely does not work as you forgot to provide /c to cmd.exe which tells it to interpret the rest of the arguments as program to run. If you do not have any reason to use cmd, I would suggest the following instead:

    proc = new QProcess;
    proc->setProgram(R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)");
    QStringList list;
    list<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
    
    proc->setArguments(QStringList()<<"/C"<<list);
    proc->start();
    

    If you still want to go with cmd I am not entirely sure how things after /c are handled. It could be that everything needs to be one single string which I don't actually know. In that case you would have to have a QStringList with only two entries: /c as the first entry and your program with all its parameters as the second. You need to figure out what cmd expects.



  • @SimonSchroeder said in How to use QProcess with setArguments() and folders with double word name?:

    This approach most likely does not work as you forgot to provide /c to cmd.exe

    Huh? Yes, he does, you quoted his:

    proc->setArguments(QStringList()<<"/C"<<list);
    

    It could be that everything needs to be one single string which I don't actually know.

    My understanding is that, unlike Linux /bin/{sh,bash} -c, cmd /c is actually followed by a line of tokens, not a single quoted argument.

    You need to figure out what cmd expects.

    That's why I told him to read carefully through the output of cmd /?, which goes through in detail the handling of /c and quoting.



  • @JonB said in How to use QProcess with setArguments() and folders with double word name?:

    Huh? Yes, he does, you quoted his:
    proc->setArguments(QStringList()<<"/C"<<list);

    Yes, you are right. I read over it too quickly.


Log in to reply