#!/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 . " : $!"; } }