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

Below is the set of files in my directory.

ITEM_1.PRODUCTS.sold

ITEM_2.PRODUCTS.sold

ITEM_3.PRODUCTS.sold

ITEM_4.PRODUCTS.sold

ITEM_5.PRODUCTS.sold

ITEM_6.PRODUCTS.sold

With below code I am able to rename the files as

ITEM_1.PRODUCTS.sold

to

ITEM_1.PRODUCTS.ordered

#/usr/bin/perl use strict; use warnings; my $dir = '/local/usr/prod/'; my $exp = 'PRODUCTS' my $code = 'ITEM_1' my @filelist =glob "${dir}*{$code}*{exp}*failed*"; foreach(@fileList){ next if -d; my$ oldname=$_; s/sold/ordered/; rename $oldname,$_ or $_=$oldname, warn "can"t rename $oldnme to $_: $!" }

However now I want my variable to be passed as

my $code = 'ITEM_1 ,ITEM_2 ,ITEM_3 ,ITEM_4 ,ITEM_5 ,ITEM_6';

and rename all the files to

ITEM_1.PRODUCTS.ordered

ITEM_2.PRODUCTS.ordered

ITEM_3.PRODUCTS.ordered

ITEM_4.PRODUCTS.ordered

ITEM_5.PRODUCTS.ordered

ITEM_6.PRODUCTS.ordered

Please help. Thanks.

Replies are listed 'Best First'.
Re: Perl Rename set of files
by NetWallah (Canon) on Sep 26, 2016 at 14:09 UTC
    Fixed multiple syntax errors, and added code to parse the new $code value.
    #/usr/bin/perl use strict; use warnings; my $code = 'ITEM_1 ,ITEM_2 ,ITEM_3 ,ITEM_4 ,ITEM_5 ,ITEM_6'; my $dir = '/local/usr/prod/'; my $exp = 'PRODUCTS'; for my $c (split /[\s,]+/, $code){ my @filelist =glob "$dir*$c*$exp*failed*"; foreach my $oldname (@filelist){ next if -d $oldname; # Update - corrected this - was missing $ +oldname! (my $newname = $oldname) =~ s/sold/ordered/; rename $oldname,$newname or warn "can't rename $oldname to $newname: $!" } }
    You should consider using @ARGV to pass arguments into your program. see perlvar.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

      Thank you...

Re: Perl Rename set of files
by choroba (Cardinal) on Sep 26, 2016 at 14:08 UTC
    Use split to split the string into a list:
    my @items = split / ,/, $code;

    Then, iterate over the array and change the name:

    for my $item (@items) { my @matching = glob "$dir/$item*.PRODUCTS.sold"; for my $file (@matching) { ( my $newname = $file ) =~ s/sold$/ordered/; rename $file, $newname; } }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,