H
H
hea0072015-01-29 16:02:40
go
hea007, 2015-01-29 16:02:40

Strange work in Go [exec] or how to correctly execute an OS command?

cmd := exec.Command("cmd", "/C", "copy", "\""+filename+"\"", "\""+dest+"\"")
      err := cmd.Run()
      if err != nil {
        log.Fatal(err)
      }

[filename] and [dest] are variables (paths) generated by the loop.
Why doesn't this code work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mayorovp, 2015-01-29
@hea007

If exec.Command receives arguments separately, then it must escape them on its own. Try removing the quotes from the last arguments.

U
uvelichitel, 2015-01-29
@uvelichitel

Judging by the path / C - this is windows. On nix systems, the code looks like this exec.Command( "cp", filename, dest)or exec.Command( "mv", filename, dest). I do not have windows to check, but try it exec.Command( "copy", filename, dest)
Well, you can manually

src, err::=os.Open(filename)
dst, err:=os.Create(dest)
size, err:= io.Copy(dst , src)
if err != nil {fmt.Println(err)}
fmt.Println("Размер", size)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question