I know that dir
requires you to double-quote a directory name that has spaces, but I'm forced to use cmd /C
which doesn't respect double-quotes
now to list a directory that has a space in its name seems impossible, whereas the CD
commands doesn't care at all about spaces, executing > CD New folder
will move you to New folder
without any issues.
EDIT
I'm trying to call it from a Go
program
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
ew folder"`
out, _ := CommandRunner(cmd)
fmt.Println(string(out))
}
it will return the filename, directory name or volume label syntax is incorrect
, any commands without double-quotes will work just fine.
execute cmd /?
and read the section that starts with If /C or /K...
that what made me say cmd /C
doesn't allow double-quotes
so it is probably an exec.Command
issue, doing the following works
command := exec.Command("cmd", "/C", "dir", "C:\Users\pyed\Desktop
ew folder")
and yes, you don't need to escape the space at all.