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. How to launch batch script from Qt?
Forum Updated to NodeBB v4.3 + New Features

How to launch batch script from Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.9k Views 2 Watching
  • 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.
  • G Offline
    G Offline
    graniteDev
    wrote on 22 Feb 2018, 20:04 last edited by
    #1

    I have several batch scripts which reside in a common folder outside the development folder my Qt app is built in.

    If I move the batch files into the same folder as my exe and run

    switchDevProcess->execute(".\\Switch_to_development.bat");
    

    The batch file runs as expected. However if I try to run the batch file from where it actually will be in production

    switchDevProcess->execute("C:\\epic\\GPS Scripts\\Switch_to_development.bat");
    

    Nothing happens. What am I missing?

    A 1 Reply Last reply 22 Feb 2018, 20:18
    0
    • G graniteDev
      22 Feb 2018, 20:04

      I have several batch scripts which reside in a common folder outside the development folder my Qt app is built in.

      If I move the batch files into the same folder as my exe and run

      switchDevProcess->execute(".\\Switch_to_development.bat");
      

      The batch file runs as expected. However if I try to run the batch file from where it actually will be in production

      switchDevProcess->execute("C:\\epic\\GPS Scripts\\Switch_to_development.bat");
      

      Nothing happens. What am I missing?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 22 Feb 2018, 20:18 last edited by
      #2

      @graniteDev said in How to launch batch script from Qt?:

      switchDevProcess->execute("C:\epic\GPS Scripts\Switch_to_development.bat");

      May the problem be related to the space in your path? Can you try a path without space?

      PS: if switchDevProcess is a QProcess, you should rather use forward slashes / instead backslashes \ as path separator.

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 22 Feb 2018, 20:20 last edited by
        #3

        Hi,

        To add to @aha_1980, you should check the status code and errorOccured signal from QProcess.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 22 Feb 2018, 20:21 last edited by mrjj
          #4

          Hi
          and be aware of space in path.

          "C:\\epic\\**GPS Scripts**\\Switch_to_development.bat"
          
          G 1 Reply Last reply 22 Feb 2018, 20:31
          0
          • G Offline
            G Offline
            graniteDev
            wrote on 22 Feb 2018, 20:26 last edited by
            #5

            I can't believe the space was the problem. I had actually tried to fix that earlier using \ to escape the space, but that didn't help, so I assumed the space was not the problem.

            Yes, removing the space worked. Problem is, I don't have control over that directory, I have to live with the space, and spaces in other directories. I'm building this right now, see if works...

            switchDevProcess->execute("C:/epic/GPS\ Scripts/Switch_to_development.bat");
            
            1 Reply Last reply
            2
            • M mrjj
              22 Feb 2018, 20:21

              Hi
              and be aware of space in path.

              "C:\\epic\\**GPS Scripts**\\Switch_to_development.bat"
              
              G Offline
              G Offline
              graniteDev
              wrote on 22 Feb 2018, 20:31 last edited by
              #6

              @mrjj
              escaping the space with \ didn't work, do I need to use the double ** like you did, or was that simply to bring it to my attention?

              M 1 Reply Last reply 22 Feb 2018, 20:32
              0
              • G graniteDev
                22 Feb 2018, 20:31

                @mrjj
                escaping the space with \ didn't work, do I need to use the double ** like you did, or was that simply to bring it to my attention?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 22 Feb 2018, 20:32 last edited by mrjj
                #7

                @graniteDev
                The ** is just for bold but i ruined it with code tags :)
                Escaping normally works. Let me check.

                Update:
                https://forum.qt.io/topic/81087/qt-create-cmd-and-execute-bat-file/2

                Seems to escape the whole path. not just space.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  graniteDev
                  wrote on 22 Feb 2018, 20:41 last edited by
                  #8

                  So I've tried encapsulating the whole string in single quotes....

                  switchDevProcess->execute("'C:/epic/GPS Scripts/Switch_to_development.bat'");
                  

                  ...and escaping the space...

                  switchDevProcess->execute("C:/epic/GPS\ Scripts/Switch_to_development.bat");
                  

                  neither worked. How is one supposed to deal with spaces in the path?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 22 Feb 2018, 20:43 last edited by
                    #9

                    Did you try my suggestions (status code, errorOccured signal) to see what is happening ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 22 Feb 2018, 20:47 last edited by
                      #10

                      Hi
                      Something like

                       "\"C:\\epic\\GPS Scripts\\Switch_to_development.bat\""
                      
                      G 1 Reply Last reply 22 Feb 2018, 20:57
                      3
                      • G Offline
                        G Offline
                        graniteDev
                        wrote on 22 Feb 2018, 20:48 last edited by
                        #11

                        I get

                        QIODevice::read: device not open

                        1 Reply Last reply
                        0
                        • M mrjj
                          22 Feb 2018, 20:47

                          Hi
                          Something like

                           "\"C:\\epic\\GPS Scripts\\Switch_to_development.bat\""
                          
                          G Offline
                          G Offline
                          graniteDev
                          wrote on 22 Feb 2018, 20:57 last edited by
                          #12

                          @mrjj said in How to launch batch script from Qt?:

                          Hi
                          Something like

                           "\"C:\\epic\\GPS Scripts\\Switch_to_development.bat\""
                          

                          YES! This worked! Thank you!

                          1 Reply Last reply
                          0

                          1/12

                          22 Feb 2018, 20:04

                          • Login

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