How to execute a simple Windows command in Golang?

后端 未结 6 1955
执念已碎
执念已碎 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:41

    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()
    
    0 讨论(0)
  • 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)
        }   
    }
    
    0 讨论(0)
  • 2020-12-08 10:57

    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("%s\n", c)
        }
    
    0 讨论(0)
  • 2020-12-08 11:03

    you can try use github.com/go-cmd/cmd module.
    because golang can not use syscall by default.

    example:

    import (
        "fmt"
        "time"
        "github.com/go-cmd/cmd"
    )
    
    func main() {
        // Start a long-running process, capture stdout and stderr
        findCmd := cmd.NewCmd("find", "/", "--name", "needle")
        statusChan := findCmd.Start() // non-blocking
    
        ticker := time.NewTicker(2 * time.Second)
    
        // Print last line of stdout every 2s
        go func() {
            for range ticker.C {
                status := findCmd.Status()
                n := len(status.Stdout)
                fmt.Println(status.Stdout[n-1])
            }
        }()
    
        // Stop command after 1 hour
        go func() {
            <-time.After(1 * time.Hour)
            findCmd.Stop()
        }()
    
        // Check if command is done
        select {
        case finalStatus := <-statusChan:
            // done
        default:
            // no, still running
        }
    
        // Block waiting for command to exit, be stopped, or be killed
        finalStatus := <-statusChan
    }
    
    0 讨论(0)
  • 2020-12-08 11:07

    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)

    0 讨论(0)
  • 2020-12-08 11:07

    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

    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)
         }
    
    0 讨论(0)
提交回复
热议问题