Here is my code:
package main
import (
"bytes"
"fmt"
"io"
"os/exec"
)
func runCommand(command string) io.Writer{
cmdName := "cmd.exe"
cmdArgs := []string{"/c", command}
fmt.Println("Running command: " + command)
cmd := exec.Command(cmdName, cmdArgs...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Run()
return cmd.Stdout
}
func main(){
fmt.Println(runCommand("dir")) // Prints the output of dir for the current directory
fmt.Println(runCommand("dir C:\\")) // Prints nothing
fmt.Println(runCommand("dir C:\\Users\\")) //Prints the output of dir for the users directory
fmt.Println(runCommand("dir C:\\..\\")) // Prints the output of dir for the C drive (What I want)
}
I'm expecting that when I execute dir C:\ That I would get the output as if I had ran in in a windows command prompt. Instead I get nothing. Intestingly, any other path when running dir works just fine. I can even see C:\ If I instead execute C:\..\ Why is this? I don't understand why this happens, and every other windows command I have given it works fine.