@ -10,6 +10,8 @@ import (
"strings"
//"log"
"os"
"sync"
"strconv"
)
var keepLongExt bool = false
@ -64,47 +66,93 @@ func dofile(i int, y string) {
fmt . Printf ( "(%d) %s -> %s\n" , i , y , newname )
}
}
var maxThreads int = 0
func main ( ) {
ar := os . Args [ 1 : ] ;
var wg sync . WaitGroup
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 ;
}
var ignore = false
for i , x := range ar {
if ignore {
ignore = false
continue
}
if ( strings . HasPrefix ( x , "--" ) ) {
switch x [ 2 : ] {
case "long" :
keepLongExt = ! keepLongExt
case "fake" :
fake = ! fake
case "recurse" :
recurse = ! recurse
case "quiet" :
noErr = ! noErr
default :
fmt . Printf ( "Unknown flag \"%s\"\n" , x )
case "long" :
keepLongExt = ! keepLongExt
case "fake" :
fake = ! fake
case "recurse" :
recurse = ! recurse
case "quiet" :
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 :
fmt . Printf ( "Unknown flag \"%s\"\n" , x )
}
} else {
if fi , err := os . Stat ( x ) ; os . IsNotExist ( err ) {
fmt . Printf ( "(%d) %s does not exist.\n" , i , x )
} 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 ( recurse ) {
filepath . Walk ( x , func ( path string , f os . FileInfo , err error ) error {
if ( ! f . Mode ( ) . IsDir ( ) ) {
dofile ( i , path )
_ dofile( i , path )
}
return nil
} )
} else {
files , err := ioutil . ReadDir ( x )
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 {
for _ , f := range files {
if ( ! f . Mode ( ) . IsDir ( ) ) {
fl := fmt . Sprintf ( "%s/%s" , x , f . Name ( ) )
dofile ( i , fl )
_ dofile( i , fl )
} else {
fmt . Printf ( "!: Ignoring %s (is directory)\n" , f . Name ( ) )
}
@ -113,11 +161,14 @@ func main() {
}
} else {
dofile( i , x )
_ dofile( i , x )
}
close ( sem )
}
}
}
wg . Wait ( )
return ;
}