@ -10,6 +10,8 @@ import (
"strings"
"strings"
//"log"
//"log"
"os"
"os"
"sync"
"strconv"
)
)
var keepLongExt bool = false
var keepLongExt bool = false
@ -64,13 +66,21 @@ func dofile(i int, y string) {
fmt . Printf ( "(%d) %s -> %s\n" , i , y , newname )
fmt . Printf ( "(%d) %s -> %s\n" , i , y , newname )
}
}
}
}
var maxThreads int = 0
func main ( ) {
func main ( ) {
ar := os . Args [ 1 : ] ;
ar := os . Args [ 1 : ] ;
var wg sync . WaitGroup
if ( len ( ar ) < 1 ) {
if ( len ( ar ) < 1 ) {
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 ] )
fmt . Printf ( "Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads <number>] <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 ;
}
}
var ignore = false
for i , x := range ar {
for i , x := range ar {
if ignore {
ignore = false
continue
}
if ( strings . HasPrefix ( x , "--" ) ) {
if ( strings . HasPrefix ( x , "--" ) ) {
switch x [ 2 : ] {
switch x [ 2 : ] {
case "long" :
case "long" :
@ -81,6 +91,17 @@ func main() {
recurse = ! recurse
recurse = ! recurse
case "quiet" :
case "quiet" :
noErr = ! noErr
noErr = ! noErr
case "threads" :
if i < len ( ar ) - 1 {
if th , err := strconv . Atoi ( ar [ i + 1 ] ) ; err == nil {
maxThreads = th
ignore = true
} else {
fmt . Printf ( "Bad threads flag" ) ;
}
} else {
fmt . Printf ( "No threads flag" )
}
default :
default :
fmt . Printf ( "Unknown flag \"%s\"\n" , x )
fmt . Printf ( "Unknown flag \"%s\"\n" , x )
}
}
@ -88,23 +109,50 @@ func main() {
if fi , err := os . Stat ( x ) ; os . IsNotExist ( err ) {
if fi , err := os . Stat ( x ) ; os . IsNotExist ( err ) {
fmt . Printf ( "(%d) %s does not exist.\n" , i , x )
fmt . Printf ( "(%d) %s does not exist.\n" , i , x )
} else {
} else {
var sem chan bool
if maxThreads > 0 {
sem = make ( chan bool , maxThreads )
} else {
sem = make ( chan bool )
}
var _dofile = func ( i int , s string ) {
if maxThreads == 1 {
dofile ( i , s )
return
}
wg . Add ( 1 )
if ( maxThreads > 0 ) {
sem <- true
}
go func ( ) {
defer wg . Done ( )
dofile ( i , s )
if ( maxThreads > 0 ) {
<- sem
}
} ( )
}
if ( fi . Mode ( ) . IsDir ( ) ) {
if ( fi . Mode ( ) . IsDir ( ) ) {
if ( recurse ) {
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 )
}
}
return nil
return nil
} )
} )
} else {
} else {
files , err := ioutil . ReadDir ( x )
files , err := ioutil . ReadDir ( x )
if err != nil {
if err != nil {
fmt . Printf ( "E: Error enumerating files: %s\n" , err )
fmt . Printf ( "E: Error enumerating files in %s : %s\n", x , err )
} else {
} else {
for _ , f := range files {
for _ , f := range files {
if ( ! f . Mode ( ) . IsDir ( ) ) {
if ( ! f . Mode ( ) . IsDir ( ) ) {
fl := fmt . Sprintf ( "%s/%s" , x , f . Name ( ) )
fl := fmt . Sprintf ( "%s/%s" , x , f . Name ( ) )
dofile ( i , fl )
_ dofile( i , fl )
} else {
} else {
fmt . Printf ( "!: Ignoring %s (is directory)\n" , f . Name ( ) )
fmt . Printf ( "!: Ignoring %s (is directory)\n" , f . Name ( ) )
}
}
@ -113,11 +161,14 @@ func main() {
}
}
} else {
} else {
dofile( i , x )
_ dofile( i , x )
}
}
close ( sem )
}
}
}
}
}
}
wg . Wait ( )
return ;
return ;
}
}