Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

renaming files...

by base_16 (Beadle)
on Aug 08, 2000 at 21:59 UTC ( [id://26836]=perlquestion: print w/replies, xml ) Need Help??

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

I wrote the following code to go through a directory of files and rename them.
The files are of the format:
A-1024-01-AM
I want to take the `-AM' off of the file.
Why doesn't this code work?
Any help would be appreciated.
$DIR="."; opendir CONCEPT, $DIR || die "no such dir"; while ($TEMP = readdir(CONCEPT)) { $TEMP1 = $TEMP; if ($TEMP1 =~ /(A-[0-9]+-[0-9]+)/) { if (rename $TEMP, $TEMP1) { print $TEMP1; } } }

Replies are listed 'Best First'.
Re: renaming files...
by lhoward (Vicar) on Aug 08, 2000 at 22:04 UTC
    ...because in your inner loop $TEMP and $TEMP1 have the same value.

    You may want to try it like this:

    #!/usr/bin/perl -w use strict; my $dir="."; opendir CONCEPT, $dir || die "no such dir"; while (my $temp = readdir(CONCEPT)){ if ($temp =~ /^(A-\d+-\d+)/) { rename $temp, $1 or die "error $! renaming $temp to $1"; } } closedir CONCEPT;
Re: renaming files...
by plaid (Chaplain) on Aug 08, 2000 at 22:06 UTC
    On the line
    if ($TEMP1 =~ /(A-[0-9]+-[0-9]+)/) {
    You want to take the output of the pattern match. $TEMP1 isn't being altered at all, it's just being matched against. The result of the pattern match is left in $1. Try this:
    $DIR="."; opendir CONCEPT, $DIR || die "no such dir"; while ($TEMP = readdir(CONCEPT)) { if ($TEMP =~ /(A-[0-9]+-[0-9]+)/) { if (rename $TEMP, $1) { print $1; } } }
    The code was editted as little as possible. Any stricting, -w'ing, and error checking is left as an excercise to the reader.
RE: renaming files...
by steveAZ98 (Monk) on Aug 09, 2000 at 00:59 UTC
    I'm not sure that you can use this but instead of writing your own script you could use rename like follows.
    rename 's/-AM$//' *
    In the directory that you need to rename the files. I believe rename is part of perl. HTH
Re: renaming files...
by ar0n (Priest) on Aug 08, 2000 at 22:01 UTC
    wrong advice, don't listen to me. nothing to see here, move along folks.

    You need to escape the '-' character, because it's interpreted as a range.

    update: that was a little short, try this: $TEMP1 =~ /^(A\-[0-9]+\-[0-9]+)\-AM/ notice the '\' characters. And the '^' so it'll only match files beginning with "A-123-123-AM"

    update numero deux: woops, guess i was wrong. need to get handle on rexeps, sorry. (and actually read posts)

    -- ar0n | Just Another Perl Joe

      Actually, you don't need to quote the '-'s, they aren't making a range. See other follow-ups for why the code isn't working.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found