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. Run .bat file

Run .bat file

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 5.2k 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.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by MartinD
    #1

    Hi,
    my .bat file on Windows pauses itself (pause; waiting for user input). I launch this bat file with:

    QStringList param;
    param << "/c" << "a.bat";
    
    QProcess process;
    process.start("cmd.exe", param);
    process.waitForStarted(-1);
    process.waitForFinished(-1);
    

    The problem is that my program doesn't open any window for this process so I'm unable to unpause my bat file. My program is therefore stuck forever on waitForFinished line.

    How to launch bat file with window?

    JonBJ 1 Reply Last reply
    0
    • M MartinD

      Hi,
      my .bat file on Windows pauses itself (pause; waiting for user input). I launch this bat file with:

      QStringList param;
      param << "/c" << "a.bat";
      
      QProcess process;
      process.start("cmd.exe", param);
      process.waitForStarted(-1);
      process.waitForFinished(-1);
      

      The problem is that my program doesn't open any window for this process so I'm unable to unpause my bat file. My program is therefore stuck forever on waitForFinished line.

      How to launch bat file with window?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @MartinD
      I believe that QProcess::startDetached() instead of QProcess::start opens a console window for you?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MartinD
        wrote on last edited by
        #3

        You are right, thank you. However, it seems that waitForFinished doesn't work with startDetached as I need. I need to sequentially execute several bat files, waiting for finish of the previous one to launch next one. How is that achievable?

        JonBJ 1 Reply Last reply
        1
        • M MartinD

          You are right, thank you. However, it seems that waitForFinished doesn't work with startDetached as I need. I need to sequentially execute several bat files, waiting for finish of the previous one to launch next one. How is that achievable?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @MartinD
          Yep, that is problematic if you have to use startDetached to get a console window....!

          While I'm thinking about it, have a read of https://stackoverflow.com/a/31724745/489865 ... Also https://stackoverflow.com/a/42292619/489865 for more bad news...

          EDIT1 https://forum.qt.io/topic/40962/qprocess-start-doesn-t-display-a-console-window-for-console-programs-windows discusses this. If I read right, it says that the non-Qt system() function does just what you want. (Yep, https://stackoverflow.com/a/45595224/489865 confirms this.) Would that be acceptable?

          Otherwise you may have to get into calling the Windows AllocConsole as per https://stackoverflow.com/a/48867208/489865 ....) Or, possibly, take the pid returned from startDetached() and do your own Window calls to wait on it. Which is why system() would be much easier :)

          1 Reply Last reply
          3
          • M Offline
            M Offline
            MartinD
            wrote on last edited by
            #5

            Thanks! I will go through it. No problem writing windows specific code.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MartinD
              wrote on last edited by MartinD
              #6

              The system call almost suits my needs, but I also need to set working dir of the process. Not sure if it is possible with system().

              EDIT: This might be the solution:

              system("cd /D C:\mypath && a.bat");

              JonBJ 1 Reply Last reply
              0
              • M MartinD

                The system call almost suits my needs, but I also need to set working dir of the process. Not sure if it is possible with system().

                EDIT: This might be the solution:

                system("cd /D C:\mypath && a.bat");

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @MartinD
                Sigh, you don't ask for much! :)

                The current directory stuff was in one of the links I gave you, from https://forum.qt.io/topic/40962/qprocess-start-doesn-t-display-a-console-window-for-console-programs-windows/11 downward. You can do it a variety of ways, https://forum.qt.io/topic/40962/qprocess-start-doesn-t-display-a-console-window-for-console-programs-windows/13 probably being the definitive explanation/suggestion. pushd does the same sort of thing as cd /d, one possible benefit being that it handles connecting to & disconnecting from network paths a bit better. Though not quite sure why the Qt program's current directory isn't suitable for your bat file (system() inherits parent's current directory, parent could chdir somewhere and then chdir back on completion)), or specify a full path to the bat file, or have the bat file do the cd, but whatever.

                P.S.
                As a by-the-by to all this: you said you were happy writing Windows specific code, ultimately all these solutions will call Windows ::CreateProcess() you could use that yourself directly to give yourself full control over everything such as detaching & creating a new console if you really find you need to/prefer that. Just a heads-up.

                M 1 Reply Last reply
                1
                • JonBJ JonB

                  @MartinD
                  Sigh, you don't ask for much! :)

                  The current directory stuff was in one of the links I gave you, from https://forum.qt.io/topic/40962/qprocess-start-doesn-t-display-a-console-window-for-console-programs-windows/11 downward. You can do it a variety of ways, https://forum.qt.io/topic/40962/qprocess-start-doesn-t-display-a-console-window-for-console-programs-windows/13 probably being the definitive explanation/suggestion. pushd does the same sort of thing as cd /d, one possible benefit being that it handles connecting to & disconnecting from network paths a bit better. Though not quite sure why the Qt program's current directory isn't suitable for your bat file (system() inherits parent's current directory, parent could chdir somewhere and then chdir back on completion)), or specify a full path to the bat file, or have the bat file do the cd, but whatever.

                  P.S.
                  As a by-the-by to all this: you said you were happy writing Windows specific code, ultimately all these solutions will call Windows ::CreateProcess() you could use that yourself directly to give yourself full control over everything such as detaching & creating a new console if you really find you need to/prefer that. Just a heads-up.

                  M Offline
                  M Offline
                  MartinD
                  wrote on last edited by
                  #8

                  @JonB Thanks for your replies. Thats all I need :)

                  There are also other than .bat commands I want to run - they need to be launched in specific directory, different from Qt app's current directory.

                  JonBJ 1 Reply Last reply
                  1
                  • M MartinD

                    @JonB Thanks for your replies. Thats all I need :)

                    There are also other than .bat commands I want to run - they need to be launched in specific directory, different from Qt app's current directory.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @MartinD
                    Just so you know: personally (I'm just a Qt user, not a Qt person) I agree that it's a bit of a mess that, say, startDetached() opens a console but cannot be waited, while start() can be waited but cannot open a console. If start() could have an option to create a console (and startDetached() have an option not to create one!) your reasonable question would never have been such a problem....

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      MartinD
                      wrote on last edited by
                      #10

                      I spent an hour debugging why this:

                      system("C:/a.bat");

                      launches console but displays nothing from a.bat in the console (no output to console)

                      and this:

                      system("cmd.exe /c C:/a.bat");

                      launches console and displays all output from a.bat in the console.

                      I noticed that the first command's output goes to Application output window in Qt Creator. Just for curiosity, why?

                      JonBJ 1 Reply Last reply
                      0
                      • M MartinD

                        I spent an hour debugging why this:

                        system("C:/a.bat");

                        launches console but displays nothing from a.bat in the console (no output to console)

                        and this:

                        system("cmd.exe /c C:/a.bat");

                        launches console and displays all output from a.bat in the console.

                        I noticed that the first command's output goes to Application output window in Qt Creator. Just for curiosity, why?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @MartinD
                        I don't know what the documentation/implementation of system() says under Windows. To run a .bat file under Windows you do have to run it via cmd.exe (or similar). Your second code obeys this. Your first one does not. Unless system() tries to run C:/a.bat by prefacing it with cmd /c for you, I think the command should fail. I don't think your a.bat will get executed, so no output. Have you checked the return result from each system() call?

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          MartinD
                          wrote on last edited by MartinD
                          #12

                          Directly "calling" bat file using system() doesn't fail. System() returns return code from the bat file. The only problem seems to be the output redirection. Now I noticed in some cases I don't see the output in launched console nor in Application output of Qt Creator.

                          So, the question now is what is the proper way to call system (particulary to launch bat files) to get process's output in its console window. I don't want my Qt app to steal the output... Launching bat files through cmd /c seems to solve this problem, but can I be sure? :)

                          JonBJ 1 Reply Last reply
                          0
                          • M MartinD

                            Directly "calling" bat file using system() doesn't fail. System() returns return code from the bat file. The only problem seems to be the output redirection. Now I noticed in some cases I don't see the output in launched console nor in Application output of Qt Creator.

                            So, the question now is what is the proper way to call system (particulary to launch bat files) to get process's output in its console window. I don't want my Qt app to steal the output... Launching bat files through cmd /c seems to solve this problem, but can I be sure? :)

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @MartinD
                            But aren't you saying that system("cmd.exe /c C:/a.bat"); does work and do what you expect? That's how I'd expect to have to invoke a .bat file, unless the system() documentation for Windows describes better.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              MartinD
                              wrote on last edited by MartinD
                              #14

                              Yes, launching .bat via cmd /c behaves as I need (its output goes to its console). I'm just asking why direct call of bat file redirects its output to my Qt app. Just in case you or somebody else know.

                              EDIT: Hmm, probably because individual commands of bat file are executed inside the process which called system().

                              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