CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Perl script to find file 1 day old and move it and rename.

 



smithia6
New User

Aug 10, 2011, 8:49 PM

Post #1 of 2 (10992 views)
Perl script to find file 1 day old and move it and rename. Can't Post

Hi I have never used perl before, I have cashe files being generated by a news feed I have on my website, they are .xml files with a long random number in front of the file name, so what I want to do is have a perl script to check a specific directory for any files a day old and move them out of there and place it into another specified directory renaming it to (yesterdays date).xml

It is only one file a day created, so I have a task scheduler on my webserver control panel so I could run this job say 1am everyday automatic.

The file names look like this : 677e8f3a88be40d3ead789faec519a9f.xml stored in /home/users/carp/autocashe

I would like it to be moved into /home/users/archive/news with a file name like 11082011.xml (date/month/year)

I have read lots of snippets on this but I have no idea where to start. It is a linux webserver

Please please please could someone help.

Regards

Ian


smithia6
New User

Aug 12, 2011, 9:16 PM

Post #2 of 2 (10925 views)
Re: [smithia6] Perl script to find file 1 day old and move it and rename. [In reply to] Can't Post

Thanks to Bill I have a script 65 Views with 1 reply so I am guessing not many people know how to do it so this is what I got from him going to try it out.

Cheers for that Bill.

use strict;
use warnings;
use Readonly;
use POSIX 'strftime';
use File::Copy;

Readonly::Scalar my $OUTPUT_DIR => '/home/users/archive/news';
Readonly::Scalar my $INPUT_DIR => '/home/users/carp/autocashe';
Readonly::Scalar my $INPUT_MASK =>
"$INPUT_DIR/"
. '?'x32
. '.xml'
;
my $output_file_name =
"$OUTPUT_DIR/"
. strftime( '%d%m%Y', localtime )
. '.xml'
;

(my $file_name) = grep { is_file_one_day_old( $_ ) } glob $INPUT_MASK;
if (!$file_name) {
die "No input file available\n";
}
move( $file_name, "$OUTPUT_DIR/$output_file_name" )
or die "Unable to move '$file_name'\n";

sub is_file_one_day_old {
return (1 == -N $_[0]);
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives