in reply to Rename file

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).

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