有没有办法逃脱“DIR”命令

时间:2015-12-21 14:06:01

标签: windows go cmd

我知道dir要求你双引号一个有空格的目录名,但我不得不使用不尊重双引号的cmd /C

现在列出一个名称中有空格的目录似乎不可能,而CD命令根本不关心空格,执行> CD New folder会将你带到New folder没有任何问题。

修改

我正试图通过Go程序

来调用它
package main

import (
    "bytes"
    "fmt"
    "os/exec"
)

// this function wraps up `exec.Command`
func CommandRunner(cmd string) ([]byte, error) {
    // make stdout and stderr buffers to save the output to
    var stdout, stderr bytes.Buffer
    // make up the command
    command := exec.Command("cmd", "/C", cmd)
    // set stdout and stderr to the command's stdout and stderr
    command.Stdout = &stdout
    command.Stderr = &stderr

    // start the command and watch for errors
    if err := command.Start(); err != nil {
        // return the err and stderr
        return stderr.Bytes(), err
    }
    // wait for the command to finish
    if err := command.Wait(); err != nil {
        // return the err and stderr
        return stderr.Bytes(), err
    }

    return stdout.Bytes(), nil
}

func main() {
    cmd := `dir "C:\Users\pyed\Desktop\new folder"`
    out, _ := CommandRunner(cmd)
    fmt.Println(string(out))

}

它将返回the filename, directory name or volume label syntax is incorrect,任何没有双引号的命令都可以正常工作。

执行cmd /?并阅读以If /C or /K...开头的部分,让我说cmd /C的内容不允许使用双引号

1 个答案:

答案 0 :(得分:0)

因此可能是exec.Command问题,执行以下工作

command := exec.Command("cmd", "/C", "dir", "C:\Users\pyed\Desktop\new folder")

是的,你根本不需要逃离这个空间。

相关问题