Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Log files in a Date / Time Stamped Directory

Reply
Thread Tools

Log files in a Date / Time Stamped Directory

 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-17-2005
"Manzoorul Hassan" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) ups.com:

Please make an effort to compose proper follow-ups by quoting an
appropriate amount of context. Please *read* and *follow* the posting
guidelines for this group.

> #!/usr/bin/perl


use strict;
use warnings;

missing.

> use Time::localtime;
> $time = localtime;


my $time = localtime;

> $date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
> $time->mday);


my $date = sprintf(
q{%04d%02d%02d},
$time->year + 1900,
$time->mon + 1,
$time->mday
);

Did you really, really want that \n in the directory name?

> opendir(DATE_DIR, "$date") || die "Cannot open directory";


opendir my $dir, $date or die "Cannot open $date: $!";

Get all the help you can from perl. See also:

perldoc -q always

Putting it all together:

#! /usr/bin/perl

use strict;
use warnings;

use Time::localtime;
my $time = localtime;

my $date = sprintf(
q{%04d%02d%02d},
$time->year + 1900,
$time->mon + 1,
$time->mday
);

opendir my $dir, $date or die "Cannot open $date: $!";

# do something

closedir $dir;

__END__

D:\Home> v
Cannot open 20050317: No such file or directory at D:\Home\v.pl line 16.

Sinan.
 
Reply With Quote
 
 
 
 
Manzoorul Hassan
Guest
Posts: n/a
 
      03-17-2005
Thanx to everyone for your paitience and help. After all of this, I
have finally realized that this does not create a new directory. I was
hoping to have a directory, and a subdirectory, created to store all
the log files. But I guess, I'll just use the Date as part of the
filename of the log.

- manzoor

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-17-2005
"Manzoorul Hassan" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> Thanx to everyone for your paitience and help.


You are welcome.

> After all of this, I have finally realized that this does not create a
> new directory.


"this" does indeed not create a directory. For that, you'll need to use
the appropriate function, as was suggested in an earlier post:

perldoc -f mkdir

If you had taken a little bit of care in composing your follow-ups
carefully, I would have known about your original purpose.

> I was hoping to have a directory, and a subdirectory,
> created to store all the log files.


What, apart from not bothering to read documentation, is stopping you
from doing that?

> But I guess, I'll just use the Date as part of the filename of
> the log.


Whatever floats your boat.

Sinan.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-17-2005
Manzoorul Hassan <(E-Mail Removed)> wrote:


> opendir(DATE_DIR, "$date") || die "Cannot open directory";

^ ^
^ ^
^ ^ perldoc -q vars

I thought you said you want to *create* a directory?

opendir, strangly enough, _opens_ a directory after it has been created.

perldoc -f mkdir


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Manzoorul Hassan
Guest
Posts: n/a
 
      03-17-2005
well, I was thining this would be similar to Files - if it doesn't
exist, create it.

But I guess Directories don't work the same way (

- manzoor

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-18-2005


Manzoorul Hassan wrote:

> use Time::localtime;
> $time = localtime;
>
> $date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
> $time->mday);


While at some level there's nothing wrong with doing it long-hand using
the basic sprintf() and addinf the necessary offsets there is a special
sprintf()-like function for fomatting localtime() to be found one of the
standard Perl modules.

For details:

perldoc -f localtime

Oh, and for $DEITY's sake get into the habit of always declaring all
variables as lexically scoped in the smallest applicable scope unless
there is a reson to do otherwise. Do this pre-emptively. Do not wait to
experience the pain that will inevitably result from not doing so.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date stamped protection on recent films? Pete L DVD Video 2 03-11-2008 12:39 AM
JPA and time stamped data Chris Ward Java 4 07-24-2007 02:12 PM
Calculating time of employee session from the log date/time stampusing perl Jayesh Kamdar Perl Misc 2 05-25-2005 12:08 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date & Time chooser for java 1.1 - using only the mouse to select time & date Chris Berg Java 0 10-27-2003 10:59 PM



Advertisments