master
Ringo Wantanabe 6 years ago
parent 61715676b9
commit 07fc573d72
No known key found for this signature in database
GPG Key ID: C1C1CD34CF2907B2

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings" "strings"
//"log" //"log"
"os" "os"
@ -13,6 +14,8 @@ import (
var keepLongExt bool = false var keepLongExt bool = false
var fake bool = false var fake bool = false
var recurse bool = false
var noErr bool = false
func last(str []string) string { func last(str []string) string {
if(len(str)<1) { if(len(str)<1) {
@ -51,7 +54,9 @@ func dofile(i int, y string) {
str := fmt.Sprintf("%x", sha) str := fmt.Sprintf("%x", sha)
newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x)) newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x))
if _, err := os.Stat(newname); !os.IsNotExist(err) { if _, err := os.Stat(newname); !os.IsNotExist(err) {
fmt.Printf("E: (%d) %s: %s alread exists.\n", i, y, newname) if(!noErr) {
fmt.Printf("E: (%d) %s: %s alread exists.\n", i, y, newname)
}
} else { } else {
if !fake { if !fake {
os.Rename(y,newname) os.Rename(y,newname)
@ -62,7 +67,7 @@ func dofile(i int, y string) {
func main() { func main() {
ar := os.Args[1:]; ar := os.Args[1:];
if (len(ar) < 1) { if (len(ar) < 1) {
fmt.Printf("Usage: %s [--long] [--fake] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n", os.Args[0]) fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n", os.Args[0])
return; return;
} }
for i, x := range ar { for i, x := range ar {
@ -72,6 +77,10 @@ func main() {
keepLongExt = !keepLongExt keepLongExt = !keepLongExt
case "fake": case "fake":
fake = !fake fake = !fake
case "recurse":
recurse = !recurse
case "quiet":
noErr = !noErr
default: default:
fmt.Printf("Unknown flag \"%s\"\n", x) fmt.Printf("Unknown flag \"%s\"\n", x)
} }
@ -80,12 +89,28 @@ func main() {
fmt.Printf("(%d) %s does not exist.\n", i, x) fmt.Printf("(%d) %s does not exist.\n", i, x)
} else { } else {
if(fi.Mode().IsDir()) { if(fi.Mode().IsDir()) {
filepath.Walk(x, func(path string, f os.FileInfo, err error) error { if(recurse) {
if(!f.Mode().IsDir()) { filepath.Walk(x, func(path string, f os.FileInfo, err error) error {
dofile(i,path) if(!f.Mode().IsDir()) {
dofile(i,path)
}
return nil
})
} else {
files, err := ioutil.ReadDir(x)
if err != nil {
fmt.Printf("E: Error enumerating files: %s\n", err)
} else {
for _, f := range files {
if(!f.Mode().IsDir()) {
fl := fmt.Sprintf("%s/%s", x, f.Name())
dofile(i,fl)
} else {
fmt.Printf("!: Ignoring %s (is directory)\n", f.Name())
}
}
} }
return nil }
})
} else { } else {
dofile(i, x) dofile(i, x)

Loading…
Cancel
Save