How can I append todays date to the file name?

D

Dariush

Hello every one.

I'm no expert at perl.

I would like to append todays date to the file name

I have many files that arrive daily

filename.pdf and so on

How can I rename the files to

filename_DDMMYY.pdf

so the output would be

filename_18112003.pdf

thank you all.
 
J

Josef Möllers

Dariush said:
Hello every one.

I'm no expert at perl.

I would like to append todays date to the file name

I have many files that arrive daily

filename.pdf and so on

How can I rename the files to

filename_DDMMYY.pdf

so the output would be

filename_18112003.pdf

thank you all.

What have you tried so far? It's a fairly simple task involving
localtime, strftime, rename and various standard Perl techniques.

perldoc -f localtime
also has an example of strftime

Josef
 
J

JR

Hello every one.

I'm no expert at perl.

I would like to append todays date to the file name

I have many files that arrive daily

filename.pdf and so on

How can I rename the files to

filename_DDMMYY.pdf

so the output would be

filename_18112003.pdf

thank you all.

This should get you started. If your files are going to be located in
in a directory that has no subdirectories, you won't or may not need
to use the File::Find module. I hope this helps.

#!/perl
use strict;
use warnings;
use File::Find;

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime (time);
$year+=1900;
$mon+=1;
my $date = "$mday$mon$year";

sub wanted {
if (-f) {
my($name, $ext) = split /\./, $_, 2;
rename $_, "$name".'_'."$date.$ext";
}
}
my $dir = 'C:/files'; # modify here as needed
&find(\&wanted, $dir);
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Dariush) wrote in
Hello every one.
Hi.

I would like to append todays date to the file name

I have many files that arrive daily

filename.pdf and so on

How can I rename the files to

filename_DDMMYY.pdf

so the output would be

filename_18112003.pdf

Download and install the Time::Format module, available on CPAN at
http://search.cpan.org/~roode/Time-Format-0.13/ . Then:

use Time::Format;
$filename = "whatever.pdf";
$new_name = $filename;
$new_name =~ s/\.(pdf|gif|jpg)$/_$time{ddmmyyyy}.$1/;
rename $filename, $new_name or die "Can't rename to $new_name: $!";

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP7qu52PeouIeTNHoEQKD5QCg4mI7KL4s65t8xbYJqnY5gZc4hLAAoMpz
GhRwrGJfF7sH/olFK0a2Brwa
=9MEB
-----END PGP SIGNATURE-----
 
T

Tintin

Dariush said:
Hello every one.

I'm no expert at perl.

I would like to append todays date to the file name

Your example inserts, not appends.
I have many files that arrive daily

filename.pdf and so on

How can I rename the files to

filename_DDMMYY.pdf

so the output would be

filename_18112003.pdf

Suggest you use a better date format YYYYMMDD so that it will easily sort.

#!/usr/bin/perl
use strict;
use POSIX;

my $today = strftime("%Y%m%d",localtime);

foreach my $file (</dir/with/files/*.pdf>) {
my ($filename,$ext) =$file =~ /(.*)\.(\w+)$/;
rename $file, "${filename}_$today.$ext" or warn "Can not rename $file
$!\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top