From d7fca734c840c3efb5f19759cbb741a5bd7eb9a5 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Tue, 25 Jul 2017 19:37:16 +0300 Subject: [PATCH] use robocopy for copying and moving on windows Mentioned in #12. --- nav.go | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/nav.go b/nav.go index 93bfdde..f6f8e6a 100644 --- a/nav.go +++ b/nav.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "sort" "strings" ) @@ -512,24 +513,44 @@ func (nav *nav) put() error { dir := nav.currDir() + var sh string var args []string - var sh string - if copy { - sh = "cp" - args = append(args, "-r") + if runtime.GOOS == "windows" { + sh = "robocopy" + if !copy { + args = []string{"/move"} + } + for _, f := range list { + stat, err := os.Stat(f) + if err != nil { + log.Printf("getting file information: %s", err) + continue + } + base := filepath.Base(f) + dest := filepath.Dir(f) + if stat.IsDir() { + exec.Command(sh, append(args, f, filepath.Join(dir.path, base))...).Run() + } else { + exec.Command(sh, append(args, dest, dir.path, base)...).Run() + } + } } else { - sh = "mv" - } + if copy { + sh = "cp" + args = append(args, "-r") + } else { + sh = "mv" + } + args = append(args, "--backup=numbered") + args = append(args, list...) + args = append(args, dir.path) - args = append(args, "--backup=numbered") - args = append(args, list...) - args = append(args, dir.path) + cmd := exec.Command(sh, args...) - cmd := exec.Command(sh, args...) - - if err := cmd.Run(); err != nil { - return fmt.Errorf("%s: %s", sh, err) + if err := cmd.Run(); err != nil { + return fmt.Errorf("%s: %s", sh, err) + } } // TODO: async?