in reply to Rename FILE to nnn-FILE

Not tested:

#!/usr/bin/perl -w use strict; my $dir = "/home/my/files"; chdir $dir or die "can't chdir $dir: $!"; my $i; do { rename $_, ++$i . "-$_" or die "can't rename: $!" } for glob "*";

Will break on directories with more than 999 files. But so will your original. :)

(This is not meant to be golf, by the way. "I'm not saying I'd code it this way, but I'm not saying I wouldn't", to paraphrase $SOMEONE_SMARTER_THAN_ME (whose identity I forget.))

Replies are listed 'Best First'.
Re^2: Rename FILE to nnn-FILE
by uksza (Canon) on Dec 29, 2004 at 23:03 UTC
    Many thanks for reply.
    Of cource yours code works very good, but - it's my fault, I didn't wrote it - makes 1-file, 2-file, 3-file... etc and I was thinking about 001-file, 002-file, 003-file ...

    regards,
    Ukasza
      Ooops, somehow I omitted this from the version I posted. Just make this change:

      my $i; # change to my $i = '000';