2015-12-21 114 views
-2

我知道dir要求你加双引号的空格目录名称,但我不得不使用cmd /C,这不涉及双引号有没有办法为“DIR”命令转义空格

现在列出名称中有空格的目录似乎是不可能的,而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

你确定'cmd/c dir“New Folder”'不起作用吗? – i486

+0

您可以在命令中使用空格。我只用这个完整的命令提示符进行了测试:'C:\ Windows \ System32> cmd/c dir“C:\ program files”/ b' –

+0

请在代码部分修改您的问题。你为什么“被迫”使用'cmd/c',你究竟想要做什么? – Magoo

回答

0

所以它可能是一个exec.Command问题,做了以下工作

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

节是的,你根本不需要逃避空间。

相关问题