Using a file's modification date in the filename

Discussion in 'Perl Misc' started by cca.johnson@gmail.com, Apr 24, 2008.

  1. Guest

    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,
     
    , Apr 24, 2008
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Apr 24, 2008
    #2
    1. Advertising

  3. Guest

    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.
     
    , Apr 24, 2008
    #3
  4. Guest

    On Apr 23, 9:25 pm, wrote:
    > 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,
     
    , Apr 24, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
  2. Andy Fish
    Replies:
    0
    Views:
    626
    Andy Fish
    Jul 20, 2004
  3. Manuel Bastioni

    Get the date of last modification of a file

    Manuel Bastioni, Sep 8, 2003, in forum: Python
    Replies:
    6
    Views:
    2,053
    Manuel Bastioni
    Sep 9, 2003
  4. Stefano Crocco
    Replies:
    0
    Views:
    150
    Stefano Crocco
    Nov 14, 2008
  5. Vance M. Allen

    File Modification Date

    Vance M. Allen, Sep 14, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    148
    Josef Moellers
    Sep 15, 2005
Loading...

Share This Page