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

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

问题描述

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

这个命令:

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

.. 输出此消息:

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

del: executable file not found in %path%

我做错了什么?

推荐答案

我遇到了和你一样的错误.但是dystroy 是正确的:你不能运行delcmd 内置的任何其他命令,因为没有del.exe 文件(或任何与此相关的其他删除可执行文件).

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天全站免登陆