如何在Golang中执行简单的Windows命令? [英] How to execute a simple Windows command in Golang?

查看:146
本文介绍了如何在Golang中执行简单的Windows命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何运行简单的 Windows命令

How to run a simple Windows command?

此命令:

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

..输出此消息:


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

del: executable file not found in %path%

我在做什么错了?

推荐答案

我遇到了与您相同的错误。
但是dystroy是正确的:您不能运行 del cmd 中内置的任何其他命令不是 del.exe 文件(或与此相关的任何其他del执行文件)。

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

我知道了

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

这篇关于如何在Golang中执行简单的Windows命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆