qiuckie for creating a file with date in name

Discussion in 'Perl Misc' started by laredotornado@zipmail.com, Jan 25, 2005.

  1. Guest

    Hi, This group has been pretty unbelievable at coming up with quick one
    or two line solutions to questions so I thought I'd throw this one out
    there. I want to create a file containing a single word "Done" for
    contents but I would like the file name to be of the form:

    File_YYYYMMDD_HHMMSS.done

    where YYYY is the four year date, MM is the month number, and so on..
    What's the quickest way I could do this?

    Thanks a million! - Dave
     
    , Jan 25, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    <> wrote in message
    news:...
    > Hi, This group has been pretty unbelievable at coming up with quick

    one
    > or two line solutions to questions so I thought I'd throw this one out
    > there. I want to create a file containing a single word "Done" for
    > contents but I would like the file name to be of the form:
    >
    > File_YYYYMMDD_HHMMSS.done
    >
    > where YYYY is the four year date, MM is the month number, and so on..
    > What's the quickest way I could do this?


    probably by using localtime()

    Something along the lines of
    my @time = localtime(time);
    my $file = sprintf("File_%4d%2d%2d_%2d%2d%2d.done",
    $time[5]+1900,$time[4]+1, @time[3,2,1,0]);

    read about localtime in
    perldoc -f localtime

    Paul Lalli
     
    Paul Lalli, Jan 25, 2005
    #2
    1. Advertising

  3. Paul Lalli wrote:
    >
    > Something along the lines of
    > my @time = localtime(time);
    > my $file = sprintf("File_%4d%2d%2d_%2d%2d%2d.done",
    > $time[5]+1900,$time[4]+1, @time[3,2,1,0]);


    That almost works but is unsightly. The OP probably wanted numbers
    padded with leading zeros not spaces.

    There is, however, another function you can use in place of sprintf()
    especially for this job to be found in one of the standard Perl modules.

    This function takes a template a bit like that used in sprintf() but the
    elements of the template directly correspond to various representations
    of bits of the time structure return by localtime().

    > read about localtime in
    > perldoc -f localtime


    And while you are there you can also read about the function I mention
    above.
     
    Brian McCauley, Jan 26, 2005
    #3
  4. Tintin Guest

    <> wrote in message
    news:...
    > Hi, This group has been pretty unbelievable at coming up with quick one
    > or two line solutions to questions so I thought I'd throw this one out
    > there. I want to create a file containing a single word "Done" for
    > contents but I would like the file name to be of the form:
    >
    > File_YYYYMMDD_HHMMSS.done
    >
    > where YYYY is the four year date, MM is the month number, and so on..
    > What's the quickest way I could do this?


    use POSIX 'strftime';
    my $file = strftime('File_%Y%m%d_%H%M%S.done',localtime);
    open FILE, ">$file" or die "Could not create $file $!\n";
     
    Tintin, Jan 26, 2005
    #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. Matt
    Replies:
    1
    Views:
    648
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,370
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    568
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    755
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    426
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page