Re: Directory creation with current Date
by ikegami (Patriarch) on Jan 19, 2005 at 02:56 UTC
|
use POSIX qw(strftime);
$parent_dir = "c:\\backups";
$formatted_date = strftime("%d-%B-%Y", localtime);
mkdir("$parent_dir/$formatted_date") or die("...: $!");
You might want to go with a format that's string-sortable, like YYYY-MM-DD:
$formatted_date = strftime("%Y-%m-%d", localtime); # YYYY-MM-DD
$formatted_date = strftime("%F", localtime); # Same as prev line
| [reply] [d/l] [select] |
|
|
$formatted_date = now_date() ;
sub now_date {
my @now = localtime ;
my $now_day = $now[3] ;
my $now_year = $now[5] + 1900 ;
my $now_month = $now[4] + 1 ;
if ($now_month < 10) { $now_month = "0".$now_month }
if ($now_day < 10) { $now_day = "0".$now_day }
my $now_scal = join "-", ( $now_year, $now_month, $now_day ) ;
return $now_scal ;
}
Update: I guess it's better with sprintf, like mkirank does :
sub now_date {
my @now = localtime ;
my $now_day = $now[3] ;
my $now_year = $now[5] + 1900 ;
my $now_month = $now[4] + 1 ;
$now_scal = sprintf "%04d-%02d-%2d", $now_year , $now_month ,$now_
+day ;
return $now_scal
}
More Update: As demerphq says it could in the end very well be much better to use POSIX qw(strftime) Coding practice evolve ! | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
|
|
|
|
|
You can do $parent_dir = "c:/backups/"; in Windows which I find easier.
| [reply] [d/l] |
Re: Directory creation with current Date
by Eyck (Priest) on Jan 19, 2005 at 09:05 UTC
|
use Date::Calc qw(Today_and_Now);
my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now();
my $dir="$day-$month-$year";
mkdir($dir);
I think this is cleaner and more readable then other proposed solutions. ( Date::Calc is a big module though ).
Now, I would recommend rethinking your 'day-month-year' style, with 'year-month-day' you get more logical view (it sorts nicer)
And another thing - with daily backups very soon you'll find out that what you got is linear list of all those backups... 300, 600, 1000 entries... not a nice thing to look at, not that easy to find what you need etc, try hierarchical structure: "$year/$month/$day/BACKUP", all you need is mkpath:
eval { mkpath($dir) };
if ($@) {
print "Couldn't create $dir: $@";
}
chdir($dir)||die;
| [reply] [d/l] [select] |
Re: Directory creation with current Date
by mkirank (Chaplain) on Jan 19, 2005 at 07:35 UTC
|
One more way to get the date
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = l
+ocaltime( time) ;
my $return ;
$mon++ ;
$year += 1900 ;
$return = sprintf "%02d-%02d-%4d",
$mday , $mon ,$year;
| [reply] [d/l] |
|
|
you could probably simplify that a bit:
my ($day,$mo,$year) = (localtime)[3,4,5];
return sprintf '%02d-%02d-%04d', $day , $mo + 1, $year + 1900;
| [reply] [d/l] |
Re: Directory creation with current Date
by DrHyde (Prior) on Jan 19, 2005 at 10:04 UTC
|
I'd be more inclined to help if you showed that you had done at least some work yourself. Perhaps showing us your code, explaining how what it did was different from what you wanted, and what steps you had tried to improve it. | [reply] |
Re: Directory creation with current Date
by ninja_byte (Acolyte) on Jan 19, 2005 at 06:16 UTC
|
I find the quickest way is to use the `date` binary. I use this pretty often when creating backups
my $date = `date|awk '{print \$2 \$3 \$6}'`;
that would set $date to 'Jan182005' today... you can add other formatting as well...
my $date = `date|awk '{print \$2 \$3 "-" \$6}'`;
Jan18-2005
etc etc...
| [reply] |
|
|
In situations where you are doing it a lot, `date ...` is not the best way to do it because you're executing an external program. Heck, you're executing two external programs using `date | awk ...`. I always use the strftime solution by force of habit, myself.
| [reply] |
|
|
| [reply] |
Re: Directory creation with current Date
by Anonymous Monk on Jan 19, 2005 at 06:38 UTC
|
Why not just do mkdir `date "+%d-%B-%Y"` | [reply] [d/l] |
|
|
This also serves as a reply to ninja_byte as well. Your solutions will not work, since you ignored the fact that Sam said he was on Win2k. While Win2k does have a date program, it does not function like the *ix date.
Example:
C:\perl\bwb>mkdir `date "+%d-%B-%Y"`
C:\perl\bwb>dir
Volume in drive C has no label.
Volume Serial Number is 34D5-1CFA
Directory of C:\perl\bwb
01/19/2005 09:36a <DIR> .
01/19/2005 09:36a <DIR> ..
01/19/2005 09:36a <DIR> +%d-%B-%Y`
...
01/19/2005 09:36a <DIR> `date
Now you have two new directories with stupid names. What was wrong with the strftime() solution that this is preferable?
/renz.
P.S. -
C:\perl\bwb>awk
'awk' is not recognized as an internal or external command,
operable program or batch file.
Update: Cleaned up html.
Perhaps: I should rephrase third sentence of first paragraph.. Seems as if I'm blaming the entire thing on `date` when that is not exactly the truth..
# rz/020805 | [reply] [d/l] [select] |