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

In reply to testing files for valid content by grashoper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.