Log files in a Date / Time Stamped Directory

M

Manzoorul Hassan

I would like to store my logs in a Directory / Subdirectory tree that
corresponds to the date / time the script is executed. For example, if
the script is executed today and noon, then the logs would be stored
under 20050317/1200/

How do I do this?

- manzoor
 
B

Brian McCauley

Manzoorul said:
I would like to store my logs in a Directory / Subdirectory tree that
corresponds to the date / time the script is executed. For example, if
the script is executed today and noon, then the logs would be stored
under 20050317/1200/

How do I do this?

What have you tried? How did it fail?
 
B

Brian McCauley

Manzoorul said:
I would like to store my logs in a Directory / Subdirectory tree that
corresponds to the date / time the script is executed. For example, if
the script is executed today and noon, then the logs would be stored
under 20050317/1200/

How do I do this?

perldoc -f localtime
 
T

Tad McClellan

Manzoorul Hassan said:
I would like to store my logs in a Directory / Subdirectory tree that
corresponds to the date / time the script is executed. For example, if
the script is executed today and noon, then the logs would be stored
under 20050317/1200/

How do I do this?


perldoc -f time
perldoc -f localtime
perldoc -f gmtime
perldoc -f mkdir
 
M

Manzoorul Hassan

OK, I tried the following for the Date and it works:

#!/usr/bin/perl -l
use Time::localtime;
$time = localtime;

printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1, $time->mday);
printf ("%02d%02d\n", $time->hour, $time->min);

But I can't store this as a value for my Date and Time Dir to be opened
/ created.

- manzoor
 
P

Paul Lalli

Manzoorul Hassan said:
OK, I tried the following for the Date and it works:

To whom are you speaking? Have you read the posting guidelines for this
group? Please quote an appropirate amount of context.
#!/usr/bin/perl -l

you forgot use strict; and use warnings;
use Time::localtime;
$time = localtime;

printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1, $time->mday);
printf ("%02d%02d\n", $time->hour, $time->min);

But I can't store this as a value for my Date and Time Dir to be opened
/ created.

Why can't you? What have you tried that failed?

Or do you just mean that you "can't" because printf() prints to the
screen instead of storing to a variable? That's what printf() does.
It's cousin sprintf(), on the other hand, returns the formatted string.
Perhaps that's what you're looking for?

Paul Lalli
 
M

Manzoorul Hassan

well, I was trying to do the following:

$date_val = printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
$time->mday);
opendir(DIR, "$date_val) || die "Cannot open Dir\n";

That didn't work. Now I am trying with the sprintf, but still not
opening.

- manzoor
 
P

Paul Lalli

Manzoorul Hassan said:
well, I was trying to do the following:

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

This is non-sensical. printf() prints a string to the screen.
sprintf() returns the string.
opendir(DIR, "$date_val) || die "Cannot open Dir\n";

That didn't work.

Of course not. It's a syntax error. Post real code.
Now I am trying with the sprintf, but still not opening.

Post your real code that is failing, along with the error messages you
are receiving. It's not possible to help you otherwise.

Paul Lalli
 
M

Manzoorul Hassan

#!/usr/bin/perl

use Time::localtime;
$time = localtime;

$date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
$time->mday);
opendir(DATE_DIR, "$date") || die "Cannot open directory";

closedir(DATE_DIR);
 
C

Chris Mattern

Manzoorul said:
#!/usr/bin/perl

use Time::localtime;
$time = localtime;

$date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
$time->mday);
opendir(DATE_DIR, "$date") || die "Cannot open directory";

closedir(DATE_DIR);

And what, exactly, is this code supposed to do? Because, as near as
I can tell, it doesn't do anything, other than die with "Cannot open
directory" if the directory in $date doesn't exist.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
A

A. Sinan Unur

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.
 
M

Manzoorul Hassan

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
 
A

A. Sinan Unur

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.
 
T

Tad McClellan

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
 
M

Manzoorul Hassan

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 :eek:(

- manzoor
 
B

Brian McCauley

Manzoorul said:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top