Important: Please read the Qt Code of Conduct -

Accessing a directory using cmd (Windows)

  • I need to access a directory by cmd. This path is acquired by reading a .txt containing multiple paths that I need.
    I keep this path that was read the .txt file in the variable path1, for example. I want to write this path in cmd, but so far I only found the myprocess.write command that I need to manually enter this command and does not accept path1 variable.
    How can I solve it?

    @QString program = "cmd.exe";
    QProcess myProcess;

    if (myProcess.waitForStarted(1000) == false)
    qDebug() << "Error: starting external program";
    qDebug() << "external program running";
    qDebug() << "read output1" << myProcess.readAllStandardOutput();
    myProcess.write(path1); //I CAN'T DO THIS !!!!!!!!!

    qDebug() << "read output2" << myProcess.readAllStandardOutput();


  • Moderators

    bq. I need to access a directory by cmd.

    Access how? Rename it, delete it, create a file inside?

    bq. myProcess.write(path1);

    You want to write a path to a cmd process? What is that suppose to do?
    What are you trying to achieve exactly?

  • I need to access the Blender directory, where there is the executable. After I was inside the directory I need to run the blender.exe and on the same line type other commands necessary for me to get what I'm trying.
    Is as follows what I want to do.
    After living in the directory I need to do this:
    @myProcess.write("blender.exe C:\Dyblend_beta3\templates\defaultDyBlend.blend -P \n");@

    The reason to want the path of the Blender directory into a variable (path1) is that in my application there are several versions of Blender I have to preselect (different directories), so I can't write it in my code, because it's something that will always change according to the user's choice.

  • You may use myProcess.setWorkingDirectory(workingDirectory);
    before myProcess.start(programm, args);

  • Moderators

    So it seems you don't need cmd (the command shell) at all.
    As msue said first set the working directory to where blender is located.
    Then start QProcess with blender.exe, not cmd.exe. Pass the parameters as a second parameter to the start(), not through write().
    write() is for something else. It writes to the process input stream so that it can read from it through stdin. You don't need it here.

  • When I do this:

    @ QProcess myProcess;
    myProcess.setWorkingDirectory(path); //I got the path previously
    QString program = "blender.exe";
    QStringList arguments;
    arguments << "D:\Dyblend_beta3\templates\defaultDyBlend.blend1" << "-P";

    if (myProcess.waitForStarted(1000) == false)
    qDebug() << "Error: starting external program";
    qDebug() << "external program running";
    qDebug() << "read output1" << myProcess.readAllStandardOutput();@

    It is the same as writing that way in cmd as follows below?
    @myProcess.write("blender.exe C:\Dyblend_beta3\templates\defaultDyBlend.blend -P \n");@

    Because when I run with that code above, the error appears: "Error: starting external program" as provided for in my code.

  • Moderators

    Code looks ok but 1 second is a small window and then only 100ms for execution. Blender is a pretty big program. Are you sure that's enough time for it to start and execute?

    You should also call the error() method when it fails to check what was the cause.

  • I tried adding the line (blender.exe) to the first parameter of QProcess :: start (), leaving the arguments QStringList empty for the second argument and nothing happened, at least it should open Blender, right?
    I first have to make sure that the code is working for me to know if the logic of my parameters is correct.
    Any idea?

  • There is another detail at the end of my code above I put qApp-> quit ();, because I do not wish my GUI remains open after this open another application.
    Is there a problem?

Log in to reply