Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with file processing using Perl

Status
Not open for further replies.

Aclems

Technical User
Mar 19, 2007
6
CA
Hi,
I am really having a tough time writing a perl script that will read all the files in a directory and transfer them to another directory on my computer.
Please, could anyone tell me what is likely wrong with the code or help me
with a better one. I am just a day old using perl. Thanks for your help.

#!/usr/local/bin/perl -w

$forcefiles="/Users/agboma/Desktop/TEST/TEST_DAILY_WB/forcing/";

opendir(FORCING, $forcefiles) || die("Cannot open directory");

@thefiles=grep !/^\.\.?$/,readdir(FORCING);

foreach $dataset (@thefiles) {
print "$dataset\n";

open(FILE,$dataset) ||die("Could not open $dataset\n");
open(OUTFILE,">/Users/agboma/Desktop/perl_learn/$dataset") ||die("Could not
open for output $dataset\n");

$line=<FILE>;

while ($line ne "") {
print OUTFILE($line);
$line=<FILE>;

}

}


I am using the terminal in MAC OSX to run perl .This is the error I get when I run the code:
.DS_Store
Use of uninitialized value in string ne at files.pl line 14.
data_33,5625_100.6875
Could not open data_33,5625_100.6875
 
Wait.. are you wanting to just copy the files?
Look at the File::Copy module.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
You can simply do
Code:
rename $dataset,"/Users/agboma/Desktop/perl_learn/$dataset";
but test it, it's not guaranteed to work on all systems (returns true or 1 for success for the sake of error handling).
Can't see the reason for error from your code, and that should be a warning only. However your code won't function, due to the end of line character.
Anyway, if you really need to discard null lines, the section starting at $line= will be more correctly and neatly written as:
Code:
  while(<FILE>){print OUTFILE unless$_ eq"\n";
[tt]while(<FILE>)[/tt] reads a line into [tt]$_[/tt] and [tt]print OUTFILE[/tt] prints [tt]$_[/tt].


Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top