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?
Qt 6.11 is out! See what's new in the release blog

How to launch batch script from Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 5.1k 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 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?

    aha_1980A 1 Reply Last reply
    0
    • G graniteDev

      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?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          0
          • G Offline
            G Offline
            graniteDev
            wrote on 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
            • mrjjM mrjj

              Hi
              and be aware of space in path.

              "C:\\epic\\**GPS Scripts**\\Switch_to_development.bat"
              
              G Offline
              G Offline
              graniteDev
              wrote on 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?

              mrjjM 1 Reply Last reply
              0
              • G graniteDev

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 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 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      Something like

                       "\"C:\\epic\\GPS Scripts\\Switch_to_development.bat\""
                      
                      G 1 Reply Last reply
                      3
                      • G Offline
                        G Offline
                        graniteDev
                        wrote on last edited by
                        #11

                        I get

                        QIODevice::read: device not open

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          Something like

                           "\"C:\\epic\\GPS Scripts\\Switch_to_development.bat\""
                          
                          G Offline
                          G Offline
                          graniteDev
                          wrote on 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

                          • Login

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