Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Rename file

by sashawn (Initiate)
on Apr 05, 2011 at 16:40 UTC ( #897551=perlquestion: print w/ replies, xml ) Need Help??
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." : $!"; } }

Comment on Rename file
Download Code
Re: Rename file
by jethro (Monsignor) on Apr 05, 2011 at 16:53 UTC
    Your post is unreadable. Edit it and use <p> for formatting paragraphs and put <code> tags around any code.
Re: Rename file
by wind (Priest) on Apr 05, 2011 at 17:04 UTC

    Perl::Tidy to the rescue.

    Here's the code in a slightly more readable format. Will let other monks carry the torch from here:

    #!/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 delete $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 delete " . $opt_s . "\\" . $file . " : $!"; } }
      thank you for making readable...this is my first time post here...messed it up...

        No problem. You can and should simply go back and edit your original post. Follow the basic example in this faq on how to add formatting using <p></p> and <code></code> tags: How do I post a question effectively?

        Additional formatting tags are available here, Perl Monks Approved HTML tags, but the former are all one normally ever needs. You can even use the reformatted text that I provided in your original post.

        Each of these faqs are linked to below the textarea where one posts a question. There are other resources available there too.

        Finally, one caveat is that you should add an "Update" note at the bottom of your post whenever you make a change. Especially to a question node.

Re: Rename file
by jpl (Monk) on Apr 05, 2011 at 17:36 UTC

    Your comments pretty much say it all

    my $path = $opt_s . "\\*.*"; $path =~ s|\\|/|g; #find the newest file my $newestfile = (sort {(stat $b)[10] <=> (stat $a)[10]} glob $path)[0];

    You are globbing over all files with extensions in $opt_s, and finding the newest. You need to restrict attention to those matching everything up to the extension in $opt_f. (Left as an exercise). I cringe a bit at re-invoking stat() for each comparison. If you simply read the $opt_s directory, and keep track of the latest file matching the $opt_f pre-extension, you can fix the problem and save a lot of stat()s. (Perhaps those are the only files you want to remove, too... hard to know from your description).

      hi...there will always be files only with extension, .xls i agree that right now,script is finding latest file instead of matching to $opt_f pre-extension. How can I match to that? I was actually trying to match that but didn't had success. Existing file is: PPSFAR65_DKMAR_CUST_QUERY-3865422.xls my $opt_f value will be PPSFAR65_DKMAR_CUST_QUERY.xls. i appreciate your suggestions. thank you.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-09-17 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (64 votes), past polls