Click here to get back home

Using a file's modification date in the filename

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Using a file's modification date in the filename cca.johnson 04-23-2008
Posted by cca.johnson on April 23, 2008, 9:57 pm
Please log in for more thread options
I want to be able to rename a file and prepend the file's modification
date at the front of the file. For example:
with a file named testme.txt and a modification date of April 1, 2006,
I want the renamed file to be named 20060401-testme.txt

What I can do is get the modification date using ctime, but it I can't
figure out how to format the output. I am able to format the date the
way I like using strftime. Here is some sample code which shows the
output I do and do not want:

#!/usr/bin/perl -w
use strict ;
use warnings ;
use POSIX qw(strftime);

# print today's date YYYYMMDD:
my $now_time = strftime "%Y%m%d", localtime;
print "I want it formatted this way:\n$now_time\n";

# print the modified date of file:
use File::stat;
use Time::localtime;
my $file = "testme.txt";
my $timestamp = ctime(stat($file)->mtime);
print "...but not this way:\n $timestamp\n";


Any assistance would be appreciated,

Posted by Jürgen Exner on April 23, 2008, 10:23 pm
Please log in for more thread options
cca.johnson@gmail.com wrote:
>I want to be able to rename a file and prepend the file's modification
>date at the front of the file. For example:
>with a file named testme.txt and a modification date of April 1, 2006,
>I want the renamed file to be named 20060401-testme.txt
>
>What I can do is get the modification date using ctime, but it I can't
>figure out how to format the output.

It seems like Date::Formatter will probably do what you are looking for.

jue

Posted by Gerry Ford on April 24, 2008, 2:34 am
Please log in for more thread options

> cca.johnson@gmail.com wrote:
>>I want to be able to rename a file and prepend the file's modification
>>date at the front of the file. For example:
>>with a file named testme.txt and a modification date of April 1, 2006,
>>I want the renamed file to be named 20060401-testme.txt
>>
>>What I can do is get the modification date using ctime, but it I can't
>>figure out how to format the output.
>
> It seems like Date::Formatter will probably do what you are looking for.

http://search.cpan.org/~stevan/Date-Formatter-0.09/lib/Date/Formatter.pm

I thought I'd chase after it, as if wanting to install the module myself.
I'm disappointed that ActiveState's PPM rarely has the modules I'm looking
for. Maybe I don't know how to look. The authors included this caveat:

For serious date/time involved work, skip my module and go straight to the
DateTime project at L<http://datetime.perl.org>. Don't even waste your time
with anything else.

, and this link to the larger question in Perl:
http://www.perl.com/pub/a/2003/03/13/datetime.html

--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."

~~ Butch Hancock



Posted by xhoster on April 23, 2008, 10:25 pm
Please log in for more thread options
cca.johnson@gmail.com wrote:
> I want to be able to rename a file and prepend the file's modification
> date at the front of the file. For example:
> with a file named testme.txt and a modification date of April 1, 2006,
> I want the renamed file to be named 20060401-testme.txt
>
> What I can do is get the modification date using ctime, but it I can't
> figure out how to format the output. I am able to format the date the
> way I like using strftime. Here is some sample code which shows the
> output I do and do not want:
>
> #!/usr/bin/perl -w
> use strict ;
> use warnings ;
> use POSIX qw(strftime);
>
> # print today's date YYYYMMDD:
> my $now_time = strftime "%Y%m%d", localtime;
> print "I want it formatted this way:\n$now_time\n";

mtime and time both use seconds since the epoch, so
it should work the same way if you just give localtime the results of
mtime instead of letting it default to using time.

my $now_time = strftime "%Y%m%d", localtime(stat($file)->mtime);

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by cca.johnson on April 23, 2008, 11:41 pm
Please log in for more thread options
On Apr 23, 9:25 pm, xhos...@gmail.com wrote:
> cca.john...@gmail.com wrote:
> > I want to be able to rename a file and prepend the file's modification
> > date at the front of the file. For example:
> > with a file named testme.txt and a modification date of April 1, 2006,
> > I want the renamed file to be named 20060401-testme.txt
>
> > What I can do is get the modification date using ctime, but it I can't
> > figure out how to format the output. I am able to format the date the
> > way I like using strftime. Here is some sample code which shows the
> > output I do and do not want:
>
> > #!/usr/bin/perl -w
> > use strict ;
> > use warnings ;
> > use POSIX qw(strftime);
>
> > # print today's date YYYYMMDD:
> > my $now_time = strftime "%Y%m%d", localtime;
> > print "I want it formatted this way:\n$now_time\n";
>
> mtime and time both use seconds since the epoch, so
> it should work the same way if you just give localtime the results of
> mtime instead of letting it default to using time.
>
> my $now_time = strftime "%Y%m%d", localtime(stat($file)->mtime);
>
> Xho

After I removed use Time::localtime, this worked without error. My
guess is that there is a conflict between POSIX qw(strftime)and
Time::localtime. Here is what I have:

#!/usr/bin/perl -w
use strict ;
use warnings ;
use POSIX qw(strftime);
use File::Copy;
use File::stat;

# print today's date YYYYMMDD:
my $now_time = strftime "%Y%m%d", localtime;
print "I want it formatted this way:\n$now_time\n";

# print the modified date of file:
my $file = "testme.txt";
my $file_time = strftime "%Y%m%d", localtime(stat($file)->mtime);
print "file $file will be renamed $file_time-$file\n";
copy("$file", "$file_time-$file") or die "can't copy file: $!";

Thank you,

Similar ThreadsPosted
Problem while changing file's modification time using utime() on windows October 19, 2004, 6:24 am
File Modification Date September 14, 2005, 12:01 am
Last "real" modification date of file June 18, 2008, 10:03 am
FAQ 5.23: How do I get a file's timestamp in perl? December 19, 2004, 12:03 am
FAQ 5.23: How do I get a file's timestamp in perl? December 25, 2004, 12:03 am
FAQ 5.24 How do I set a file's timestamp in perl? January 31, 2005, 12:03 am
FAQ 5.23 How do I get a file's timestamp in perl? February 15, 2005, 6:03 pm
FAQ 5.25 How do I set a file's timestamp in perl? April 27, 2005, 11:03 am
FAQ 5.24 How do I get a file's timestamp in perl? May 15, 2005, 5:03 am
FAQ 5.25 How do I set a file's timestamp in perl? July 13, 2005, 4:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap