Created
June 7, 2015 18:37
-
-
Save radutopala/9790111f519b28971223 to your computer and use it in GitHub Desktop.
Moving files with go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"os" | |
s "strings" | |
"github.com/kr/fs" | |
) | |
func Copy(dst, src string) error { | |
in, err := os.Open(src) | |
if err != nil { | |
return err | |
} | |
defer in.Close() | |
out, err := os.Create(dst) | |
if err != nil { | |
return err | |
} | |
defer out.Close() | |
_, err = io.Copy(out, in) | |
cerr := out.Close() | |
if err != nil { | |
return err | |
} | |
return cerr | |
} | |
func main() { | |
walker := fs.Walk("./photos") | |
for walker.Step() { | |
if err := walker.Err(); err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
continue | |
} | |
stat := walker.Stat() | |
if stat.IsDir() == false && !s.HasSuffix(stat.Name(), ".json") && !s.HasSuffix(stat.Name(), "-edited.jpg") { | |
fmt.Println(walker.Path()) | |
fmt.Println(walker.Stat()) | |
err := Copy("./copied/"+stat.Name(), walker.Path()) | |
fmt.Fprintln(os.Stderr, err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment