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"
"fmt"
"io"
"io/ioutil"
"strings"
//"log"
"os"
@ -13,6 +14,8 @@ import (
var keepLongExt bool = false
var fake bool = false
var recurse bool = false
var noErr bool = false
func last(str []string) string {
if(len(str)<1) {
@ -51,7 +54,9 @@ func dofile(i int, y string) {
str := fmt.Sprintf("%x", sha)
newname := fmt.Sprintf("%s%s%s", loc, str, extractExt(x))
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 {
if !fake {
os.Rename(y,newname)
@ -62,7 +67,7 @@ func dofile(i int, y string) {
func main() {
ar := os.Args[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;
}
for i, x := range ar {
@ -72,6 +77,10 @@ func main() {
keepLongExt = !keepLongExt
case "fake":
fake = !fake
case "recurse":
recurse = !recurse
case "quiet":
noErr = !noErr
default:
fmt.Printf("Unknown flag \"%s\"\n", x)
}
@ -80,12 +89,28 @@ func main() {
fmt.Printf("(%d) %s does not exist.\n", i, x)
} else {
if(fi.Mode().IsDir()) {
filepath.Walk(x, func(path string, f os.FileInfo, err error) error {
if(!f.Mode().IsDir()) {
dofile(i,path)
if(recurse) {
filepath.Walk(x, func(path string, f os.FileInfo, err error) error {
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 {
dofile(i, x)

Loading…
Cancel
Save