|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Consolidating many CSV files into one
My following code supposed to read the many text files from my C drive and create a new text file, containing all the text files read. But my code seems to stop reading at "while (<INFILE>)". What is the problem?? I am using ActivePerl 5.10.0
$counter = 1; $countArray = 0; $monthfolder = "Mar08"; $lastdayofmonth = 31; $newArray = 0; $dir = "C:/WLBX/"; opendir(BIN, $dir) or die "Can't open $dir: $!"; while( defined ($scanDirectory = readdir BIN) ) { unless (($scanDirectory eq ".") || ($scanDirectory eq "..")) { if (-d $scanDirectory) { $directoryArray[$newArray] = "$scanDirectory"; $newArray++; } } } closedir(BIN); while ($countArray < $newArray) { unlink ("C:/WLBX/$directoryArray[$countArray]/$monthfolder/$monthfolder.csv"); while ($counter <= $lastdayofmonth) { $file = "C:/WLBX/$directoryArray[$countArray]/$monthfolder/Daily/$counter.CSV"; print "\n$file"; $fname = "C:/WLBX/$directoryArray[$countArray]/$monthfolder/"; $fname .= "$monthfolder.csv"; open (FILE,">>$fname") or die "Couldn't create file $fname"; open(INFILE, $file); # request an exclusive lock on the file. flock(INFILE, LOCK_EX); # read in each line from the file while (<INFILE>) { ######### it does not run this loop ######## # $_ is the line that <INFILE> has set. #print "$_"; if ($counter == 1) { next unless $. > 2; } else { next unless $. > $newline; } print "$."; print FILE $_; } # close of while lockbox.txt creation $endline = $.; $newline = $endline + 2; $counter = $counter +1; } # close of while counter flock(INFILE, LOCK_UN); close(INFILE); close(FILE); $countArray++; } |
|
#2
|
||||
|
||||
|
please post your code in [code ][ /code] tags without spaces
Code:
open(INFILE, $file) or die $!;
# request an exclusive lock on the file.
flock(INFILE, LOCK_EX); # I'm not 100%, but I don't think flock works on windows
# read in each line from the file
while (<INFILE>) {
print "$."; #moved this line above the conditional
if ($counter == 1) {
next unless $. > 2;
} else {
next unless $. > $newline; #shouldn't this be initialised before the loop
}
print "$.";
print FILE $_;
} # close of while lockbox.txt creation
$endline = $.;
$newline = $endline + 2;
$counter = $counter +1;
} # close of while counter
flock(INFILE, LOCK_UN);
close(INFILE);
close(FILE);
$countArray++;
}
__________________
--Ax without exception, there is no rule ... The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones ![]() 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski |
|
#3
|
|||
|
|||
|
It still doesn't print "$." after I moved it before the "if ($counter == 1)"
As for the $newline, the first loop is always $counter == 1, therefore it should take "next unless $. > 2;", and when it comes to the 2nd loop, $newline already has a value assigned at the end of the while loop "$newline = $endline + 2;", therefore, I do not see the need to initialize the $newline at the beginning. Even if I intialized it, the problem still persists. |
|
#4
|
|||
|
|||
|
hi Axweildr, I have just figured out what's wrong, I put the wrong path assigned to $file. The rest of the code is actually working
Thank you very much for trying to help |
|
#5
|
||||
|
||||
|
that'd be the bit in red I take it?
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Consolidating many CSV files into one |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|