Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Rename FILE to nnn-FILE

by uksza (Monsignor)
on Dec 29, 2004 at 22:41 UTC ( #418113=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Rename FILE to nnn-FILE
Download Code
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 BrowserUk (Pope) 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 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('*') );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://418113]
Approved by Eimi Metamorphoumai
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-12-21 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls