Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Install the Linux rename utility' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Install the Linux rename utility
Authored by: juanfal on Jul 14, '05 10:38:16AM
An easier of reading, powerful and short is the next one (manage Tcl regexps and admits -recursion, -capitularization....)
#!/usr/bin/env tclsh
# Juan Falgueras, 2004-12-28
#   -- añado capitularizar.. pero falla
# «» regexp -indices {[:alpha:]} 01-pepe.mp3 que
# 1
# «» set que
# 3 3
# «» regexp -indices {[:alpha:]} ./01-pepe.mp3 que
# 1
# «» set que
# 5 5
# Además file rename se niega a cambiar a minúsculas


set Capitularizar 0
set capitularizar 0
set minusculizar 0
set recursivo 0
set pat ""
set reemp ""


if {$argc == 3 && [lindex $argv 0] == "-r"} {
    set recursivo 1
    set pat [lindex $argv 1]
    set reemp [lindex $argv 2]
} elseif {$argc == 1 && [lindex $argv 0] == "-C"} {
    set Capitularizar 1
} elseif {$argc == 1 && [lindex $argv 0] == "-c"} {
    set capitularizar 1
} elseif {$argc == 1 && [lindex $argv 0] == "-m"} {
    set minusculizar 1
} elseif {$argc != 2} {
    puts stderr "USAR: $argv0 '(p)at' '..\1..'"
    return 1
} else {
    set pat [lindex $argv 0]
    set reemp [lindex $argv 1]
}


proc procDir {d} {
    global recursivo pat reemp capitularizar Capitularizar minusculizar

    foreach f [glob "$d/*"] {
        if {$recursivo && [file isdirectory $f]} {
            procDir $f
        }
        if {$Capitularizar} {
            set fichnombre [file tail $f]
            regexp -indices {[:alpha:]} $fichnombre indiceLetra
            set nunom [string totitle $fichnombre [lindex $indiceLetra 0] end]
            file rename -force "[file tail $f]" "$nunom"
        } elseif {$minusculizar} {
            eval exec mv \"$f\" \"[file dirname $f]/[string tolower [file tail $f]]\"
        } elseif [regexp -- "$pat" "[file tail $f]"] {
            regsub -all -- "$pat" "[file tail $f]" "$reemp" nunom
            puts "'$f' --> '[file dirname $f]/$nunom'"
            file rename "$f" "[file dirname $f]/$nunom"
        } 
    }
    return 0
}

procDir .


[ Reply to This | # ]