sashawn has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I have a file with name: PPSFAR65_DKMAR_CUST_QUERY-3865422.xls.
Everyday it needs to be renamed to PPSFAR65_DKMAR_CUST_QUERY.xls. It was working fine till I started getting couple more files: PPSFAR66_MARBS_CUST_QUERY-3865420.xls and PPSFAR67_PMNT_ON_SIS_INVC-3865419.xls.
With the addition of the new files, even though I pass parameter, PPSFAR65_DKMAR_CUST_QUERY.xls, it goes and updates file PPSFAR67_PMNT_ON_SIS_INVC-3865419.xls as that is the latest file.
Here is code I have:
#!/usr/bin/perl # Syntax : /../perl /.../PS_file_Rename.pl -s /..../Source_directory - +f filename.xls # .. -> path of Perl.exe # ... -> path of the script PS_file_Rename.pl # -s option - Source_Folder # -f option - Filename to use for renaming use Getopt::Std; use Data::Dumper; use POSIX qw(strftime); use vars qw($opt_s $opt_f); getopts('s:f:'); $ff="$opt_s"."\\"."$opt_f"; #if the file name already exists delete it if (-e $ff) { unlink($ff) or warn scalar localtime(time()), " WARN Can't del +ete $ff : $!"; } my $path = $opt_s."\\*.*"; $path =~ s|\\|/|g; #find the newest file my $newestfile = (sort{(stat $b)[10] <=> (stat $a)[10]}glob $path)[0]; #rename the newest file to the name passed rename($newestfile,$ff) or warn scalar localtime(time()), " WARN Can' +t rename $newestfile : $!"; @files = glob $opt_s."\\*.*"; foreach my $file (@files) { if($file ne $ff) { unlink($file) or warn scalar localtime(time()), " WARN Can't d +elete ".$opt_s."\\".$file." : $!"; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Rename file
by wind (Priest) on Apr 05, 2011 at 17:04 UTC | |
by sashawn (Initiate) on Apr 05, 2011 at 17:23 UTC | |
by wind (Priest) on Apr 05, 2011 at 18:17 UTC | |
Re: Rename file
by jpl (Monk) on Apr 05, 2011 at 17:36 UTC | |
by sashawn (Initiate) on Apr 05, 2011 at 19:06 UTC | |
Re: Rename file
by jethro (Monsignor) on Apr 05, 2011 at 16:53 UTC |
Back to
Seekers of Perl Wisdom