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." : $!";
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.