education about perl, part 2

Posted on 2005-04-27
Last Modified: 2010-03-05
I'm working on a perl script that is going to grab a bunch of files from a directory, rename them according to what they are called, create a new timestamp-based folder and move the newly renamed files into the new time-stamped based directory.  I understand software concepts, but the regular expression stuff is still very difficult for me.

I am now creating the directory and reading the source directory and successfully moving the files.  Now I need to rename the files.

Here's the file names before rename:

Here's the desired file name after the rename:
Server01__NEWFIRST.2005. -> NovOval_2005-02-14_16-10.rpy
Server04__NEWFIRST.2005. -> IntRC_2005-02-14_15-28.rpy

Now i have a bunch of other files with different server names but this should give you the gist of what I'm trying to do.  I'm currently looping through the directory and grabbing each file individually, hoping to rename them according to the convention above, and then copy them over.

Any suggestions?  Thanks in advance!
Question by:will1383
    1 Comment
    LVL 84

    Accepted Solution

    my %tr=(
    Server01_ => 'NovOval',
    Server04_ => 'IntRC',
    my $servers=join',',keys %tr;
    for( <{$servers}_NEWFIRST.*.rpy> ){
       (my $n=$_) =~ s/(Server\d+_)_\w*\.(\d{4})\.(\d\d)\.(\d\d)\.(\d\d)\./$tr{$1}_$2-$3-$4_$5-/; rename $_,$n or warn "rename $_,$n $!";

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now