On Mon, 04 Jan 2010 18:02:38 +0100, Dário "P." <fbsd.questions.l...@gmail.com> 
wrote:
> Hello,
> 
> I have one directory with some pictures that I wanna rename (I use csh,
> don't know if that matters).
> 
> For exemple, I have:
> 
> b.jpg
> bs.jpg
> bsd.jpg
> 
> And I wanna change to:
> 
> bsd1.jpg
> bsd2.jpg
> bsd3.jpg
> 
> I really appreciate if someone can help me. :)

I know it's quite ugly and complicatedly written, but maybe
the attached script will help. It "just works", but the more
I look at it, the more I wish I hadn't written it, or just
used sh and its printf %03d mechanism. :-)

Keep in mind that the script follows the csh's sorting
order to resolve *, which usually is lexicographical
order.

For example

        97.jpg
        98.jpg
        99.jpg
        100.jpg

will, after issuing

        renumber bla jpg

result in

        bla_01.jpg = 100.jpg
        bla_02.jpg = 97.jpg
        bla_03.jpg = 98.jpg
        bla_04.jpg = 99.jpg

So if you wish to do some file preparation, know that the
powerful Midnight Commander can do this for you (select and
PF6).

Here's the script now. Put it in ~/bin (and add this directory
to your $PATH) as "renumber" (or any name you like), give it +x
permissions and "rehash" to make it available to the C shell.
Then, use "renumber <prefix> <suffix>". It will process ALL
files in the current directory (as I said: ugly as sin).


#!/bin/csh
if ( $1 == "" || $2 == "" ) then
        echo "Usage: renumber <base> <extension>"
        echo "       Target form: <base>_nn[n].<extension>"
        echo "       For 1 to 99 files: nn; for more than 99 files: nnn"
        exit 1
endif

set n = `ls -l | wc | awk '{print $1}'`
set num = `expr $n - 1`
echo "${num} files to handle."

set base = $1
set extn = $2
set n = 0
foreach f ( *.${extn} )
        set n = `expr $n + 1`
        if ( ${num} > 99 ) then
                if ( ${%n} == 1 ) then
                        mv "${f}" "${base}_00${n}.${extn}"
                else if ( ${%n} == 2 ) then
                        mv "${f}" "${base}_0${n}.${extn}"
                else
                        mv "${f}" "${base}_${n}.${extn}"
                endif
        else
                if ( ${%n} == 1 ) then
                        mv "${f}" "${base}_0${n}.${extn}"
                else
                        mv "${f}" "${base}_${n}.${extn}"
                endif
        endif
end


-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"

Reply via email to