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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

append multiple file

Status
Not open for further replies.

noyk

MIS
Feb 20, 2009
23
MY
i need a quick script to open multiple files in a directory and copy all the content to a new file.

thanks in advance
 
It would help if you show what you have tried so far and if this is school work, it really frowned upon here. Psuedo Perl code below. This sol can change depending on your dir / File match variables...etc.

Code:
$dirPath = "/path/do/Dir";
opendir DIR,"$dirPath" or die "Error Message:$!\n";
@filesToRead = (grep/somePattern/i,readdir DIR);
closedir DIR;

if (@filesToRead) {
   for ($i=0;$i<=(scalar @filesToRead);$i++) {
        Several Options can go here, depending on your OS.
   }
} else {
   print "Error Message.\n";
  }
 
thanks, i got up to here so far.. but it stl doesnt seems to work. btw i am actually a biologist, so i actually dont do a lot of programming

$dirPath = "c:/d_melanogaster/";
opendir DIR,"$dirPath" or die "Error Message:$!\n";
@filesToRead = (grep/N/i,readdir DIR);
closedir DIR;

if (@filesToRead) {
for ($i=0;$i<=(scalar @filesToRead);$i++) {
open (APPEND, ">>@filesToRead") or die "cant open";
for $line (<APPEND>) {
print $line;
}
}
} else {
print "Error Message.\n";
}

 
You are not creating nor opening the new destination file, and you open each source file for append, so you can't read anything.
Code:
$dirPath = "c:/d_melanogaster/";
opendir DIR,"$dirPath" or die "Error Message:$!\n";
@filesToRead = (grep/N/i,readdir DIR);
closedir DIR;
open(DEST,">destination.fil") or die "cant open destination.fil";
if(@filesToRead) {
  for(@filesToRead){
    open(SRC,$_) or die "cant open $_";
    while(<SRC>) {
      print $_;
    }
    close SRC;
  }   
}else{
  print "Error Message.\n";
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
thanks but the script cannot open the second file
 
Code:
use strict;
use warnings;
my $dirPath = 'c:/d_melanogaster/';
opendir DIR, $dirPath or die "Error Message: $!\n";
my @filesToRead = (grep/N/i,readdir DIR);
closedir DIR;
open(DEST,">>", "your/path/to/outputfile") or die "cant open destination.fil: [red]$![/red]";
for(@filesToRead){
    open(SRC,$_) or die "cant open $_: [red]$![/red]";
    print DEST <SRC>;
    close SRC;
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
a similar error occur in Kevin's and prex's code
i still cannot open the files

open(SRC,$_) or die "cant open $_: $!";
 
Don't want to discourage you, but I've got the impression that you are going too fast and that you should deepen a bit your knowledge of perl programming by trying simple scripts.
You should first of all tell us what kind of error is displayed when the script dies: to show this message was purposely one of Kevin's suggestions.
You are trying to open files that contain a 'N' or 'n' in their names: do you have any such files in your directory?
Add a line
[tt]print "***@filesToRead***\n";[/tt]
after the closedir and report how many files are listed or whatever you see between the stars.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
thanks for the advice.. i definitely had a lot of catching up to do.

the error read
cant open NC_004353.faa: No such file or directory at append3 line9.

this is line 9
open(SRC,$_) or die "cant open $_: $!";

i add the statement print "***@filesToRead***\n";
and get every files that i want like this

***NC_004353.faa NC_004354.faa NT_033777.faa NT_033778.faa NT_033779.faa NT_037436.faa NW_630092.faa NW_630146.faa NW_630232.faa NW_630247.faa NW_630641.faa NW_630973.faa NW_631340.faa NW_631342.faa NW_631567.faa NW_631583.faa NW_631589.faa NW_631661.faa NW_631676.faa NW_632023.faa NW_632145.faa NW_632213.faa NW_632216.faa NW_632272.faa NW_632371.faa NW_632490.faa NW_632496.faa NW_632565.faa NW_632607.faa NW_632608.faa NW_632614.faa NW_632649.faa NW_632695.faa NW_632699.faa NW_632702.faa NW_632708.faa NW_632743.faa NW_632745.faa NW_632748.faa NW_632749.faa NW_632750.faa NW_632755.faa NW_632758.faa NW_632759.faa NW_632766.faa NW_632771.faa NW_632773.faa NW_632774.faa NW_632780.faa NW_632783.faa NW_632786.faa NW_632787.faa NW_632793.faa NW_632794.faa NW_632800.faa NW_632803.faa NW_632806.faa NW_632808.faa NW_632809.faa NW_632810.faa NW_632811.faa NW_632812.faa NW_632814.faa NW_632815.faa NW_632816.faa NW_632818.faa NW_632821.faa NW_632824.faa NW_632825.faa NW_632836.faa NW_632838.faa NW_632839.faa NW_632840.faa NW_632841.faa NW_632842.faa NW_632843.faa NW_632844.faa***

 
How about
Code:
use strict;
use warnings;
my $dirPath = 'c:/d_melanogaster/';
opendir DIR, $dirPath or die "Error Message: $!\n";
my @filesToRead = (grep/N/i,readdir DIR);
closedir DIR;
open(DEST,">>", "your/path/to/outputfile") or die "cant open destination.fil: $!";
for(@filesToRead){
    open(SRC,[red]$dirpath.[/red]$_) or die "cant open [red]$dirpath[/red]$_: $!";
    print DEST <SRC>;
    close SRC;
}

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
this one works perfectly after correcting for the variable $dirPath..

so the reason, the first attempts fail is because I did not specify the path to my directory?

thanks a lot everybody
 
so the reason, the first attempts fail is because I did not specify the path to my directory?

Thats correct. If you use only the filename you must be in the same directory as the file. Using the full path you can open a file regardless of the current directory.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top