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. Accessing a directory using cmd (Windows)
QtWS25 Last Chance

Accessing a directory using cmd (Windows)

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 1.9k 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.
  • N Offline
    N Offline
    Neutrox
    wrote on last edited by
    #1

    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;
    myProcess.start(program);

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

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

    qApp->quit();@

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Neutrox
        wrote on last edited by
        #3

        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 importEngine.py \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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          msue
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              Neutrox
              wrote on last edited by
              #6

              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 importEngine.py";
              myProcess.start(program,arguments);

              if (myProcess.waitForStarted(1000) == false)
              qDebug() << "Error: starting external program";
              
              else
              qDebug() << "external program running";
              
              //myProcess.waitForReadyRead(100);
              myProcess.waitForFinished(100);
              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 importEngine.py \n");@

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

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Neutrox
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Neutrox
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0

                    • Login

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