How to execute a simple Windows command in Golang?

后端 未结 6 1972
执念已碎
执念已碎 2020-12-08 10:06

How to run a simple Windows command?

This command:

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

.. outputs th

6条回答
  •  盖世英雄少女心
    2020-12-08 10:55

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

提交回复
热议问题