This code works on windows in bash terminal, but doesn't work in cmd terminal.

func main () {
fmt.Print ("Enter source directory path:")
inBuf:= bufio.NewReader (os.Stdin)
inDir, _:= inBuf.ReadString ('\ n')
inDir= strings.Replace (inDir, "\ n", "", -1)
files, err:= ioutil.ReadDir (inDir)
if err!= nil {
    log.Fatalf ("error reading files from dir% s:% s", inDir, err.Error ())
for _, file:= range files {
    if file.IsDir () {
    fmt.Printf ("filename:% s", file.Name ())
fmt.Println ("ok")

Result in cmd:

C: \ Users \ dev \ go \ src \ awesomeProject4 >
Enter source directory path: C: \ files
: The filename, directory name, or volume label syntax is incorrect.

Tell me what needs to be changed in the code or how to enter the path to the directory in stdin for it to work in cmd.exe?

  • Answer # 1

    On Windows, line feed is two characters\ r \ n, not one\ nas in Unix. Accordingly, you need to take this into account -replace the line in your codeinDir= strings.Replace (inDir, "\ n", "", -1)two are:

    inDir= strings.TrimSuffix (inDir, "\ n")
    inDir= strings.TrimSuffix (inDir, "\ r")

    thanks, it helped!

    Elena2021-02-23 23:13:07