@ -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 ) {
if ( ! noErr ) {
fmt . Printf ( "E: (%d) %s: %s alread exists.\n" , i , y , newname )
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) error s.\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,6 +89,7 @@ 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 ( ) ) {
if ( recurse ) {
filepath . Walk ( x , func ( path string , f os . FileInfo , err error ) error {
filepath . Walk ( x , func ( path string , f os . FileInfo , err error ) error {
if ( ! f . Mode ( ) . IsDir ( ) ) {
if ( ! f . Mode ( ) . IsDir ( ) ) {
dofile ( i , path )
dofile ( i , path )
@ -87,6 +97,21 @@ func main() {
return nil
return nil
} )
} )
} else {
} 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 ( ) )
}
}
}
}
} else {
dofile ( i , x )
dofile ( i , x )
}
}