How to execute a simple Windows command in Golang?
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
add a comment |
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
Please don't refer to commands likeDIR
orRM
as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOSRM
command, you'd have to usentvdm.exe
, Windows NT Virtual DOS Machine
– stacker-baka
Mar 26 '18 at 11:49
add a comment |
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
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
windows go cmd
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 likeDIR
orRM
as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOSRM
command, you'd have to usentvdm.exe
, Windows NT Virtual DOS Machine
– stacker-baka
Mar 26 '18 at 11:49
add a comment |
Please don't refer to commands likeDIR
orRM
as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOSRM
command, you'd have to usentvdm.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
add a comment |
4 Answers
4
active
oldest
votes
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)
}
}
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 likecmd := exec.Command("cmd", "killall opera")
Doesn't work for me.
– Alessandro Resta
Jun 16 '15 at 10:07
on a mac something likecommand := 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" meanscmd
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
|
show 2 more comments
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)
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
add a comment |
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()
add a comment |
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)
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
}
}
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 likecmd := exec.Command("cmd", "killall opera")
Doesn't work for me.
– Alessandro Resta
Jun 16 '15 at 10:07
on a mac something likecommand := 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" meanscmd
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
|
show 2 more comments
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)
}
}
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 likecmd := exec.Command("cmd", "killall opera")
Doesn't work for me.
– Alessandro Resta
Jun 16 '15 at 10:07
on a mac something likecommand := 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" meanscmd
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
|
show 2 more comments
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)
}
}
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)
}
}
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 likecmd := exec.Command("cmd", "killall opera")
Doesn't work for me.
– Alessandro Resta
Jun 16 '15 at 10:07
on a mac something likecommand := 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" meanscmd
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
|
show 2 more comments
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 likecmd := exec.Command("cmd", "killall opera")
Doesn't work for me.
– Alessandro Resta
Jun 16 '15 at 10:07
on a mac something likecommand := 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" meanscmd
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
|
show 2 more comments
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
answered Oct 22 '12 at 14:47
Yster
1,61321933
1,61321933
add a comment |
add a comment |
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)
}
add a comment |
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)
}
add a comment |
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)
}
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)
}
answered Nov 1 '18 at 13:59
Constantin Konstantinidis
12
12
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please don't refer to commands like
DIR
orRM
as DOS commands. The ones you want to use are Windows terminal/console commands. If you really wanted to use DOSRM
command, you'd have to usentvdm.exe
, Windows NT Virtual DOS Machine– stacker-baka
Mar 26 '18 at 11:49