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

Appending same file content to a unique file in each sub-directory 1

Status
Not open for further replies.

RVSachin

Technical User
Nov 12, 2001
77
IN
Hi,

I have a file abc.prj in each of many subdirectories.
I need to append the contents of myfil.txt to every abc.prj file in the same subdirectories.

Can somebody please help me out on this?

The structure is as follows:

Main ---|
|subdir1 --> abc.prj
|subdir2 --> abc.prj
|subdir3 --> abc.prj
|subdir4 --> abc.prj


|subdirn --> abc.prj


Each abc.prj should be appended with the contents of myfil.txt AND the Directory structure should be retained.


Thanks,

RV
 
Not tested...
Code:
#!/usr/bin/perl -w
use strict;

my $orgfile = "abc.prj";
my $addfile = "myfil.txt";
my $resfile = "total.txt";

open ORG, "<$orgfile" or die "can not open $orgfile";
my @result = <ORG>;
close ORG or die "can not close $orgfile";

open ADD, "<$addfile" or die "can not open $addfile";
@result = (@result, <ADD>);
close ADD or die "can not close $addfile";

open RES, ">$resfile" or die :can not open $resfile";
foreach my $item (@result)
{
   print RES $item;
}
close RES or die "can not close $resfile";
 
I'm assuming there's one copy of myfil.txt in your main directory, not one in each subdirectory. If the latter, it's an easy change to make.

I'm getting the name of the file to append to (abc.prj), the file to append (myfil.txt) and the name of the main directory from the command line, but you could hardcode those into the program.

I make a backup copy of each $targetfile named $targetfile.bak, but you could probably come up with something better as the file extension and either hardcode it or let the user specify it.

Lightly tested. (It worked. [thumbsup2])
Code:
#!perl
# Append contents of $appendfile to $targetfile in each subdirectory of $maindir.

use strict;
use warnings;

use File::Copy;
use Cwd qw(chdir getcwd);

# $targetfile: name of file to append to
# $appendfile: file whose contents we're appending to $targetfile
# $maindir (optional): name of main directory in which $appendfile will be found
#    and in whose subdirectories $targetfile will be found.  
#    If $maindir is not specified, it's assumed to be the current directory.

my ($targetfile, $appendfile, $maindir) = (shift, shift, shift);
die qq(\$targetfile and \$appendfile must be specified on command line.\n)
    unless $targetfile && $appendfile;

# chdir to $maindir unless we're already there
my $thisdir = getcwd;
$maindir ||= $thisdir;
unless ($maindir eq $thisdir) {
    chdir $maindir || die qq(Can't chdir to "$maindir".\n);
}
$thisdir = getcwd;

# Get contents of $appendfile
open(APPENDFILE, $appendfile) ||
    die qq(Can't open append file "${maindir}/$appendfile" for read.\n);
my @append = <APPENDFILE>;

opendir(MAINDIR, "$thisdir") || die qq(Can't open "$thisdir".\n);
while (my $dir = readdir(MAINDIR)) {
    next if ($dir eq ".") || ($dir eq "..");
    next unless -d $dir;
    unless (open(TARGET, "${dir}/$targetfile")) {
        warn qq(Can't open target file "${dir}/$targetfile" for read.\n);
        next;
    }
    my @target = <TARGET>;
    unless (close(TARGET)) {
        warn qq(Can't close target file "${dir}/$targetfile" after reading.\n);
        next;
    }
    # Make backup copy of $targetfile, named $targetfile.bak
    # NOTE: File::Copy and File::Move return 1 for success, 0 for failure!
    my $result = copy("${dir}/$targetfile", "${dir}/${targetfile}.bak");
    unless ($result) {
        warn qq(Couldn't copy "${dir}/$targetfile" to "${dir}/${targetfile}.bak".\n);
        next;
    }
    unless (open(TARGET, ">${dir}/$targetfile")) {
        warn qq(Can't open target file "${dir}/$targetfile" for write.\n);
        next;
    }
    # Write out original contents of $targetfile followed by $appendfile,
    # overwriting original $targetfile
    print TARGET (@target, @append);
    unless (close(TARGET)) {
        warn qq(Can't close target file "${dir}/$targetfile" after writing.\n);
        next;
    }
}
closedir(MAINDIR) || die qq(Couldn't close "$thisdir".\n);

__END__
 
Code:
#!/usr/bin/perl

chdir "/Users/duncancarr/Desktop/RV/";

undef $/;
open (MYFIL, "< myfil.txt");
$myfil_contents = <MYFIL>;
close MYFIL;
$/ = "\n";

while (<subdir*>) {
  open (APPEND, ">> $_/abc.prj");
  print APPEND "\n$myfil_contents\n";
  close APPEND;
}


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top