How to execute a simple Windows command in Golang?












20














How to run a simple Windows command?



This command:



exec.Command("del", "c:\aaa.txt")


.. outputs this message:




del: executable file not found in %path%




What am I doing wrong?










share|improve this question
























  • Please don't refer to commands like DIR or RM as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOS RM command, you'd have to use ntvdm.exe, Windows NT Virtual DOS Machine
    – stacker-baka
    Mar 26 '18 at 11:49
















20














How to run a simple Windows command?



This command:



exec.Command("del", "c:\aaa.txt")


.. outputs this message:




del: executable file not found in %path%




What am I doing wrong?










share|improve this question
























  • Please don't refer to commands like DIR or RM as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOS RM command, you'd have to use ntvdm.exe, Windows NT Virtual DOS Machine
    – stacker-baka
    Mar 26 '18 at 11:49














20












20








20


7





How to run a simple Windows command?



This command:



exec.Command("del", "c:\aaa.txt")


.. outputs this message:




del: executable file not found in %path%




What am I doing wrong?










share|improve this question















How to run a simple Windows command?



This command:



exec.Command("del", "c:\aaa.txt")


.. outputs this message:




del: executable file not found in %path%




What am I doing wrong?







windows go cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 3:19









Rene Knop

1,3403622




1,3403622










asked Oct 22 '12 at 9:18









Yster

1,61321933




1,61321933












  • Please don't refer to commands like DIR or RM as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOS RM command, you'd have to use ntvdm.exe, Windows NT Virtual DOS Machine
    – stacker-baka
    Mar 26 '18 at 11:49


















  • Please don't refer to commands like DIR or RM as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOS RM command, you'd have to use ntvdm.exe, Windows NT Virtual DOS Machine
    – stacker-baka
    Mar 26 '18 at 11:49
















Please don't refer to commands like DIR or RM as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOS RM command, you'd have to use ntvdm.exe, Windows NT Virtual DOS Machine
– stacker-baka
Mar 26 '18 at 11:49




Please don't refer to commands like DIR or RM as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOS RM command, you'd have to use ntvdm.exe, Windows NT Virtual DOS Machine
– stacker-baka
Mar 26 '18 at 11:49












4 Answers
4






active

oldest

votes


















36














I got the same error as you.
But dystroy is correct: You can't run del or any other command built into cmd because there is no del.exe file (or any other del-executable for that matter).



I got it to work with:



package main

import(
"fmt"
"os/exec"
)

func main(){
c := exec.Command("cmd", "/C", "del", "D:\a.txt")

if err := c.Run(); err != nil {
fmt.Println("Error: ", err)
}
}





share|improve this answer





















  • Well done ANisus. Thanks for the help!
    – Yster
    Oct 22 '12 at 14:41










  • What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
    – Alessandro Resta
    Jun 16 '15 at 10:07












  • on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
    – ffel
    Jul 16 '15 at 11:40










  • Can you explain the "/C" portion of the command arguments?
    – user2179522
    Sep 3 '15 at 3:13










  • @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
    – ANisus
    Sep 3 '15 at 6:37



















6














You need a Windows cmd to execute your dir command.



Try this :



cmd := exec.Command("cmd", "/C dir").Output()


(sorry, no Windows computer to check it right now)






share|improve this answer





















  • Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
    – Yster
    Oct 22 '12 at 12:21






  • 2




    Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
    – ANisus
    Oct 22 '12 at 14:27



















1














Found another solution too. Create a batch file that contains the following: del c:aaa.txt



Then call it like this:



exec.Command("c:\del.bat").Run()





share|improve this answer





























    0














    In case you need the output of cmd:



    if c, err := exec.Command("cmd","/c","del","a.txt").CombinedOutput(); err != nil {
    log.Fatal(err)
    } else {
    fmt.Printf("%sn", c)
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13008255%2fhow-to-execute-a-simple-windows-command-in-golang%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      36














      I got the same error as you.
      But dystroy is correct: You can't run del or any other command built into cmd because there is no del.exe file (or any other del-executable for that matter).



      I got it to work with:



      package main

      import(
      "fmt"
      "os/exec"
      )

      func main(){
      c := exec.Command("cmd", "/C", "del", "D:\a.txt")

      if err := c.Run(); err != nil {
      fmt.Println("Error: ", err)
      }
      }





      share|improve this answer





















      • Well done ANisus. Thanks for the help!
        – Yster
        Oct 22 '12 at 14:41










      • What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
        – Alessandro Resta
        Jun 16 '15 at 10:07












      • on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
        – ffel
        Jul 16 '15 at 11:40










      • Can you explain the "/C" portion of the command arguments?
        – user2179522
        Sep 3 '15 at 3:13










      • @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
        – ANisus
        Sep 3 '15 at 6:37
















      36














      I got the same error as you.
      But dystroy is correct: You can't run del or any other command built into cmd because there is no del.exe file (or any other del-executable for that matter).



      I got it to work with:



      package main

      import(
      "fmt"
      "os/exec"
      )

      func main(){
      c := exec.Command("cmd", "/C", "del", "D:\a.txt")

      if err := c.Run(); err != nil {
      fmt.Println("Error: ", err)
      }
      }





      share|improve this answer





















      • Well done ANisus. Thanks for the help!
        – Yster
        Oct 22 '12 at 14:41










      • What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
        – Alessandro Resta
        Jun 16 '15 at 10:07












      • on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
        – ffel
        Jul 16 '15 at 11:40










      • Can you explain the "/C" portion of the command arguments?
        – user2179522
        Sep 3 '15 at 3:13










      • @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
        – ANisus
        Sep 3 '15 at 6:37














      36












      36








      36






      I got the same error as you.
      But dystroy is correct: You can't run del or any other command built into cmd because there is no del.exe file (or any other del-executable for that matter).



      I got it to work with:



      package main

      import(
      "fmt"
      "os/exec"
      )

      func main(){
      c := exec.Command("cmd", "/C", "del", "D:\a.txt")

      if err := c.Run(); err != nil {
      fmt.Println("Error: ", err)
      }
      }





      share|improve this answer












      I got the same error as you.
      But dystroy is correct: You can't run del or any other command built into cmd because there is no del.exe file (or any other del-executable for that matter).



      I got it to work with:



      package main

      import(
      "fmt"
      "os/exec"
      )

      func main(){
      c := exec.Command("cmd", "/C", "del", "D:\a.txt")

      if err := c.Run(); err != nil {
      fmt.Println("Error: ", err)
      }
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 22 '12 at 14:26









      ANisus

      36.2k19107129




      36.2k19107129












      • Well done ANisus. Thanks for the help!
        – Yster
        Oct 22 '12 at 14:41










      • What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
        – Alessandro Resta
        Jun 16 '15 at 10:07












      • on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
        – ffel
        Jul 16 '15 at 11:40










      • Can you explain the "/C" portion of the command arguments?
        – user2179522
        Sep 3 '15 at 3:13










      • @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
        – ANisus
        Sep 3 '15 at 6:37


















      • Well done ANisus. Thanks for the help!
        – Yster
        Oct 22 '12 at 14:41










      • What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
        – Alessandro Resta
        Jun 16 '15 at 10:07












      • on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
        – ffel
        Jul 16 '15 at 11:40










      • Can you explain the "/C" portion of the command arguments?
        – user2179522
        Sep 3 '15 at 3:13










      • @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
        – ANisus
        Sep 3 '15 at 6:37
















      Well done ANisus. Thanks for the help!
      – Yster
      Oct 22 '12 at 14:41




      Well done ANisus. Thanks for the help!
      – Yster
      Oct 22 '12 at 14:41












      What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
      – Alessandro Resta
      Jun 16 '15 at 10:07






      What if i would want to use that on mac os? something like cmd := exec.Command("cmd", "killall opera") Doesn't work for me.
      – Alessandro Resta
      Jun 16 '15 at 10:07














      on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
      – ffel
      Jul 16 '15 at 11:40




      on a mac something like command := exec.Command("bash", "-c", "<your command>") will work
      – ffel
      Jul 16 '15 at 11:40












      Can you explain the "/C" portion of the command arguments?
      – user2179522
      Sep 3 '15 at 3:13




      Can you explain the "/C" portion of the command arguments?
      – user2179522
      Sep 3 '15 at 3:13












      @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
      – ANisus
      Sep 3 '15 at 6:37




      @user2179522 "/C" means cmd should carry out the following command and then terminate; in this case "del". But it could be any built-in command.
      – ANisus
      Sep 3 '15 at 6:37













      6














      You need a Windows cmd to execute your dir command.



      Try this :



      cmd := exec.Command("cmd", "/C dir").Output()


      (sorry, no Windows computer to check it right now)






      share|improve this answer





















      • Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
        – Yster
        Oct 22 '12 at 12:21






      • 2




        Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
        – ANisus
        Oct 22 '12 at 14:27
















      6














      You need a Windows cmd to execute your dir command.



      Try this :



      cmd := exec.Command("cmd", "/C dir").Output()


      (sorry, no Windows computer to check it right now)






      share|improve this answer





















      • Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
        – Yster
        Oct 22 '12 at 12:21






      • 2




        Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
        – ANisus
        Oct 22 '12 at 14:27














      6












      6








      6






      You need a Windows cmd to execute your dir command.



      Try this :



      cmd := exec.Command("cmd", "/C dir").Output()


      (sorry, no Windows computer to check it right now)






      share|improve this answer












      You need a Windows cmd to execute your dir command.



      Try this :



      cmd := exec.Command("cmd", "/C dir").Output()


      (sorry, no Windows computer to check it right now)







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Oct 22 '12 at 9:36









      Denys Séguret

      274k52576593




      274k52576593












      • Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
        – Yster
        Oct 22 '12 at 12:21






      • 2




        Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
        – ANisus
        Oct 22 '12 at 14:27


















      • Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
        – Yster
        Oct 22 '12 at 12:21






      • 2




        Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
        – ANisus
        Oct 22 '12 at 14:27
















      Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
      – Yster
      Oct 22 '12 at 12:21




      Hi dystroy. I tried it and variations on it, but it doesn't seem to work. It runs, but nothing happens. For instance: exec.Command("cmd /c del c:\bbb\aaa.txt") . Thanks for trying.
      – Yster
      Oct 22 '12 at 12:21




      2




      2




      Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
      – ANisus
      Oct 22 '12 at 14:27




      Since you are deleting from C:, make sure you run your Go-program with privileges to delete from C:
      – ANisus
      Oct 22 '12 at 14:27











      1














      Found another solution too. Create a batch file that contains the following: del c:aaa.txt



      Then call it like this:



      exec.Command("c:\del.bat").Run()





      share|improve this answer


























        1














        Found another solution too. Create a batch file that contains the following: del c:aaa.txt



        Then call it like this:



        exec.Command("c:\del.bat").Run()





        share|improve this answer
























          1












          1








          1






          Found another solution too. Create a batch file that contains the following: del c:aaa.txt



          Then call it like this:



          exec.Command("c:\del.bat").Run()





          share|improve this answer












          Found another solution too. Create a batch file that contains the following: del c:aaa.txt



          Then call it like this:



          exec.Command("c:\del.bat").Run()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 22 '12 at 14:47









          Yster

          1,61321933




          1,61321933























              0














              In case you need the output of cmd:



              if c, err := exec.Command("cmd","/c","del","a.txt").CombinedOutput(); err != nil {
              log.Fatal(err)
              } else {
              fmt.Printf("%sn", c)
              }





              share|improve this answer


























                0














                In case you need the output of cmd:



                if c, err := exec.Command("cmd","/c","del","a.txt").CombinedOutput(); err != nil {
                log.Fatal(err)
                } else {
                fmt.Printf("%sn", c)
                }





                share|improve this answer
























                  0












                  0








                  0






                  In case you need the output of cmd:



                  if c, err := exec.Command("cmd","/c","del","a.txt").CombinedOutput(); err != nil {
                  log.Fatal(err)
                  } else {
                  fmt.Printf("%sn", c)
                  }





                  share|improve this answer












                  In case you need the output of cmd:



                  if c, err := exec.Command("cmd","/c","del","a.txt").CombinedOutput(); err != nil {
                  log.Fatal(err)
                  } else {
                  fmt.Printf("%sn", c)
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 1 '18 at 13:59









                  Constantin Konstantinidis

                  12




                  12






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13008255%2fhow-to-execute-a-simple-windows-command-in-golang%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python