http://www.perlmonks.org?node_id=418113

uksza has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

is any shorter way to do it:
#!/usr/bin/perl $dir = "/home/my/files" ; chdir $dir or die "$!"; opendir(DIR,$dir ) or die "$!"; $i = 1; @files = grep { !/^\./ } readdir(DIR); foreach (@files) { $new = $_; $new =~ s/$new/sprintf ("%03d-%s",$i,$_)/e; rename $_,$new; $i++; }
Maybe any oneliner?
I ask about this becouse I'm still learning how to "thinking and speaking Perl". Now, I'm still thinking, I suppose, like C-coder (look please at my 417228 and 417727).
I'm affraid I couldn't use all force of Perl syntax and idioms, am I right?

Uksza

Replies are listed 'Best First'.
Re: Rename FILE to nnn-FILE
by BrowserUk (Patriarch) on Dec 30, 2004 at 03:01 UTC

    As a "one-liner" (with caveats), this would do the trick:

    perl -wle "$x='000'; $x++ and $o=$_ and s[/(.*$)][/$x-$1] and rename($ +o,$_) or warn $! for glob qq[$ARGV[0]/*]" dir

    but trying to remember how to type that is too hard, so you'd want to make it a script, at which point your original is much nicer.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Rename FILE to nnn-FILE
by gaal (Parson) on Dec 29, 2004 at 22:52 UTC
    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.))

      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';
Re: Rename FILE to nnn-FILE
by Eimi Metamorphoumai (Deacon) on Dec 29, 2004 at 23:02 UTC
    Here's how I would write it, compromising between brevity and readability. (untested)
    #!/usr/bin/perl use strict; use warnings; my $dir = "/home/my/files" ; chdir $dir or die "$!"; opendir(DIR,$dir ) or die "$!"; my $i = 1; for (grep { !/^\./ } readdir(DIR)) { rename $_,sprintf ('%03d-%s',$i++,$_) or warn "Couldn't rename $_: + $!"; }
    I think you may be trying too hard to use perlisms (like the regexp replace). Often, it's better to just look for simpler ways to do things.
      I think you may be trying too hard to use perlisms (like the regexp replace). Often, it's better to just look for simpler ways to do things.

      But I'm dreaming about myself as Great_Saint_YAPH_Perl_Lover ;-))
Re: Rename FILE to nnn-FILE
by saskaqueer (Friar) on Dec 30, 2004 at 07:02 UTC

    For what it's worth, here's how I'd probably have written it (something like this anyhow). I'm not too sure about the glob. I've heard about how it's better to use the opendir/readdir combo, so I'm not too sure what to think of that.

    #!perl -w use strict; chdir('bar') or die("chdir() failed: $!\n"); my $cnt; rename( $_, sprintf('%03d-%s', ++$cnt, $_) ) or warn("rename() failed: $!\n") for ( grep { ! /\A\./ } glob('*') );