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
Forum Updated to NodeBB v4.3 + New Features

Run .bat file

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 5.5k 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 MartinD
    8 Nov 2018, 10:23

    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?

    J Offline
    J Offline
    JonB
    wrote on 8 Nov 2018, 10:35 last edited by JonB 11 Aug 2018, 10:50
    #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 8 Nov 2018, 11:20 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 8 Nov 2018, 14:14 last edited by MartinD 11 Aug 2018, 14:19
        #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");

        J 1 Reply Last reply 8 Nov 2018, 14:57
        0
        • M MartinD
          8 Nov 2018, 14:14

          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");

          J Offline
          J Offline
          JonB
          wrote on 8 Nov 2018, 14:57 last edited by JonB 11 Aug 2018, 15:02
          #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 8 Nov 2018, 15:11
          1
          • J JonB
            8 Nov 2018, 14:57

            @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 8 Nov 2018, 15:11 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.

            J 1 Reply Last reply 8 Nov 2018, 15:53
            1
            • M MartinD
              8 Nov 2018, 15:11

              @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.

              J Offline
              J Offline
              JonB
              wrote on 8 Nov 2018, 15:53 last edited by JonB 11 Aug 2018, 15:53
              #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 8 Nov 2018, 20:48 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?

                J 1 Reply Last reply 8 Nov 2018, 20:55
                0
                • M MartinD
                  8 Nov 2018, 20:48

                  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?

                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Nov 2018, 20:55 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 8 Nov 2018, 20:59 last edited by MartinD 11 Aug 2018, 21:04
                    #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? :)

                    J 1 Reply Last reply 8 Nov 2018, 21:06
                    0
                    • M MartinD
                      8 Nov 2018, 20:59

                      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? :)

                      J Offline
                      J Offline
                      JonB
                      wrote on 8 Nov 2018, 21:06 last edited by JonB 11 Aug 2018, 21:06
                      #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 8 Nov 2018, 21:09 last edited by MartinD 11 Aug 2018, 21:16
                        #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

                        13/14

                        8 Nov 2018, 21:06

                        • Login

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