I have a series of text files I would like to open and verify they either contain a full record or not.
records are comma delimited as follows
example.. of 1 record.
4/22/2009 1:36:02 PM,29.281263,23.429273,4.420066,29.219497,2.847088,11.055522,0.159151,AAR
my current code so far which reads in directories and list of files I also have a small script which shows a single files content with labels, I would like to modify this file so that it checks for a full record or not, if not it should skip the line and do the next or possibly trash the line.
<code>
opendir(DIR, "c:\\mlx\\") || die "can't opendir: $!";
my @list = grep { $_ ne '.' && $_ ne '..'&& $_ ne 'copy.pl' }
@list=readdir(DIR);
foreach my $name (@list)
{
$path="c:\\mlx\\$_";
shift @list;
print $name, "\n";
opendir(DIR, "$path\\$name");
my @files=grep {$_ ne '.' && $_ ne '..'}
@files=readdir(DIR);
foreach $file(@files){
print "filename is ", $file, " \n";
print "and I am in directory ", $name, " \n";
}
}
example of echoing contents of file..
open (FILE, '04-22-2009.txt');
while (<FILE>) {
chomp;
($timeoftest, $login, $searchload, $searchresults, $searchdetails,$add
+listing,$editlisting,$logout,$sitecode) = split(",");
print "time of test: $timeoftest\n";
print "login: $login\n";
print "searchload: $searchload\n";
print "searchresults: $searchresults\n";
print "searchdetails: $searchdetails\n";
print "addlisting: $addlisting\n";
print "editlisting: $editlisting\n";
print "logout: $logout\n";
print "sitecode: $sitecode\n";
print "---------\n";
}
close (FILE);
exit;
I tried using the sample provided by lostjimmy but I don't totally understand what its doing now.. all it does is give me some file names and the following output
here is the code now.. and my output is below it. I would like for it to go through all directories and open all the files testing each and reporting if one does not contain the proper number of fields.
#use strict;
#use warnings;
opendir(DIR, "c:\\mlx\\") || die "can't opendir: $!";
my @list = grep { $_ ne '.' && $_ ne '..'&& $_ ne 'copy.pl' } readdir(
+DIR);
foreach my $name (@list)
{
$path="c:\\mlx\\$_";
shift @list;
print $name, "\n";
opendir(DIR, "$path\\$name");
my @files=grep {$_ ne '.' && $_ ne '..'}readdir(DIR);
foreach $file(@files){
print "filename is ", $file, " \n";
print "and I am in directory ", $name, " \n";
chdir $name;
print "I am in directory $name";
open (FILE, '$file')|| die "Cannot open file $file";
while (<FILE>) {
chomp;
($timeoftest, $login, $searchload, $searchresults, $searchdetails,$add
+listing,$editlisting,$logout,$sitecode) = split(",");
print "time of test: $timeoftest\n";
print "login: $login\n";
print "searchload: $searchload\n";
print "searchresults: $searchresults\n";
print "searchdetails: $searchdetails\n";
print "addlisting: $addlisting\n";
print "editlisting: $editlisting\n";
print "logout: $logout\n";
print "sitecode: $sitecode\n";
print "---------\n";
}
close (FILE);
exit;
}
}
output..
C:\>perl 2.pl
03-27-2009.txt
04-02-2009.txt
04-07-2009.txt
04-09-2009.txt
04-13-2009.txt
04-15-2009.txt
04-17-2009.txt
04-21-2009.txt
04-23-2009.txt
AAR
filename is 04-16-2009.txt
and I am in directory AAR
Cannot open file 04-16-2009.txt at 2.pl line 22.
I am in directory AAR