Kaushal Shriyan wrote:
Hi,
I have binary files 20080630 under a particular directory
(/mnt/data1/adserver/BinaryAdLogs) and there is a directory by the
name 2008_6_30 in /mnt/data1/adserver/DailyLogs
Basically i need to check the directory whether it exists in
/mnt/data1/adserver/DailyLogs for all the binary files located in
/mnt/data1/adserver/BinaryAdLogs
You can check the existance of directories with "-d" and that of files
with "-f".
For example, if you want to check if all files exist in both
directories, you can do something like this (untested code from memory):
use strict;
use warnings;
opendir(my $dfh, "/mnt/data1/adserver/BinaryAdLogs");
while((my $fname = readdir($dfh)) {
next if($fname eq "." || $fname eq "..");
my $newfname = "/mnt/data1/adserver/DailyLogs/" . $fname;
if(!-f $newfname) {
# Oooops, do something
}
}
closedir($dfh);
Similar, for your special needs this should be similar to this:
use strict;
use warnings;
opendir(my $dfh, "/mnt/data1/adserver/BinaryAdLogs");
my $year, $month, $day;
while((my $fname = readdir($dfh)) {
# Ignore standard unix dirs
next if($fname eq "." || $fname eq "..");
if($fname =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
# Get date parts from filename
($year, $month, $day) = ($1, $2, $3);
} else {
print "Filename does not match required prototype: $fname\n";
next;
}
# remove leading zero from month and day
$day = 0 + $day;
$month = 0 + $month;
# construct the directory name
my $dirname = $year . "_" . $month . "_" . $day;
# make it absolute path
my $newfname = "/mnt/data1/adserver/DailyLogs/" . $dirname;
# and check for its existance
if(!-d $newfname) {
# Oooops, directory does not exist - do something
}
}
closedir($dfh);
LG
Rene
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/