in reply to Rename file

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

Replies are listed 'Best First'.
Re^2: Rename file
by sashawn (Initiate) on Apr 05, 2011 at 17:23 UTC
    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.