如何在 Golang 中执行一个简单的 Windows 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13008255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:00:21  来源:igfitidea点击:

How to execute a simple Windows command in Golang?

windowsgocmd

提问by Yster

How to run a simple Windows command?

如何运行一个简单的Windows 命令

This command:

这个命令:

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

.. outputs this message:

.. 输出此消息:

del: executable file not found in %path%

del: 在 %path% 中找不到可执行文件

What am I doing wrong?

我究竟做错了什么?

回答by ANisus

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

我和你有同样的错误。但是dystroy 是正确的:您不能运行del或内置任何其他命令,cmd因为没有del.exe文件(或任何其他可执行的文件)。

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)
    }   
}

回答by Denys Séguret

You need a Windows cmd to execute your dircommand.

您需要一个 Windows cmd 来执行您的dir命令。

Try this :

尝试这个 :

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

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

(抱歉,现在没有 Windows 计算机可以检查它)

回答by Constantin Konstantinidis

In case you need the output of cmd:

如果您需要 cmd 的输出:

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

回答by Yster

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

也找到了另一个解决方案。创建一个包含以下内容的批处理文件:del c:\aaa.txt

Then call it like this:

然后像这样调用它:

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

回答by Bruno Alexandre Moreira Pincho

Ok let's see, according to the documentation, in windows, processes receive commands as a single line string and do some parsing of their own. Exec's Command function builds the command string by combining all arguments together using CommandLineToArgvW, that despite being the most common quoting algorithm doesn't work for every application. Applications like msiexec.exe and cmd.exe use an incompatible unquoting algorithm, hence the extra mile. Heres a different example using powershell

好的,让我们看看,根据文档,在 Windows 中,进程将命令作为单行字符串接收并进行一些自己的解析。Exec 的 Command 函数通过使用 CommandLineToArgvW 将所有参数组合在一起来构建命令字符串,尽管它是最常见的引用算法,但并不适用于每个应用程序。像 msiexec.exe 和 cmd.exe 这样的应用程序使用不兼容的取消引用算法,因此需要额外的努力。这是使用 powershell 的不同示例

package main

import (
        "os/exec"
        "fmt"
        "log"
        )
func main() {
     out, err := exec.Command("powershell","remove-item","aaa.txt").Output()
     if err != nil {
         log.Fatal(err)
     } else {
         fmt.Printf("%s",out)
     }