N
N
Nicholas2019-10-05 15:22:21
go
Nicholas, 2019-10-05 15:22:21

golang. Windows. Why doesn't creating a folder through a new cmd window work?

Task: make a folder through a new cmd window, I know that there is a standard library for this, but the point is to understand how to work with the standard flows of a process launched in a new console window, and not in creating a folder.
Please advise what is wrong.

package main

import(
    "os/exec"
    "log"
)

func main(){    
    cmdNewWindow := exec.Command("cmd", "/C", "start cmd.exe") 
    pipe, err := cmdNewWindow.StdinPipe()
    if err != nil {
           log.Fatal("Error: ",err)
    }
    pipe.Write([]byte("md C:\\1"))
    pipe.Close()
    cmdNewWindow.Run()
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-10-05
@grinat

func ExampleCmd_StdinPipe() {
  cmd := exec.Command("cat")
  stdin, err := cmd.StdinPipe()
  if err != nil {
    log.Fatal(err)
  }

  go func() {
    defer stdin.Close()
                // тута писать туда
    io.WriteString(stdin, "values written to stdin are passed to cmd's standard input")
  }()

  out, err := cmd.CombinedOutput()
  if err != nil {
    log.Fatal(err)
  }

  fmt.Printf("%s\n", out)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question