#!/usr/bin/perl -w #use strict; use warnings; use File::stat; use lib "c:/strawberry/perl/site/lib"; #This program will extract the header information in 10K and 10Q filings #as well as file sizes. #Specify the directory containing the files that you want to read; my $files_dir = 'C:\Test'; #Specify the directory containing the results/output; my $write_dir = 'C:\Test Output'; #Open the directory containing the files you plan to read; opendir(my $dir_handle, $files_dir) or die "Can't open directory $!"; #Initialize file counter variable; my $file_count = 0; #Loop for reading each file in the input directory; while (my $filename = readdir($dir_handle)) { next unless -f $files_dir.'/'.$filename; print "Processing $filename\n"; #Initialize the variable names. my $line_count=0; my $cik=-99; my $form_type=""; my $report_date=-99; my $file_date=-99; my $name=""; my $sic=-99; my $sic1=-99; my $HTML=0; #Open the input file; open my $FH_IN, '<',$files_dir.'/'.$filename or die "Can't open $filename"; #Within the file loop, read each line of the current file; while (my $line = <$FH_IN>) { next unless -f $files_dir.'/'.$filename; #Open the ouput file; open my $FH_OUT, '>',$write_dir.'/'.$filename or die "Can't open file $filename"; #The following steps obtain basic data from various lines in the file; if($line=~m/^\s*CENTRAL\s*INDEX\s*KEY:\s*(\d*)/m){$cik=$1;} if($line=~m/^\s*FORM\s*TYPE:\s*(.*$)/m){$form_type=$1;} if($line=~m/^\s*CONFORMED\s*PERIOD\s*OF\s*REPORT:\s*(\d*)/m){$report_date=$1;} if($line=~m/^\s*FILED\s*AS\s*OF\s*DATE:\s*(\d*)/m){$file_date=$1;} if($line=~m/^\s*COMPANY\s*CONFORMED\s*NAME:\s*(.*$)/m){$name=$1;} if($line=~m/^\s*STANDARD\s*INDUSTRIAL\s*CLASSIFICATION:\s*(\d*)/m){$sic=$1;} if($line=~m/^\s*STANDARD\s*INDUSTRIAL\s*CLASSIFICATION:.*?\[(\d{4})/m){$sic1=$1;} #Update line counter; ++$line_count; } #Now that we have reached EOF< grab file size, and save the results; my $filesize = -s $files_dir . '/' . $filename; my $sb = stat($files_dir . '/' . $filename)->size; print $FH_OUT "$cik,$form_type,$report_date,$file_date,$name,$sic,$sic1,$filesize,$sb\n"; #close $FH_IN or die "unable to close $filename"; #Update file_counter; #Update file counter; ++$file_count; #Save/write results/output; print "$count lines read from $filename\n"; closedir($dir_handle); close($FH_OUT); }