Re: Newbie needs help replacing files in a directory
by CombatSquirrel (Hermit) on Oct 30, 2003 at 20:03 UTC
|
Of course TIMTOWTDI, but here is a pure Perl solution:
#!perl -w
use strict;
my $dir = 'C:/somedir/';
my $file = 'C:/otherdir/t.htm';
my $criteria = qr/\.html?$/;
my $num = 0;
opendir DIR, $dir or die "Could not open '$dir': $!";
my @files = readdir DIR;
closedir DIR;
open INFILE, '<', $file or die "Could not open '$file': $!";
# binmode INFILE; # for non-text file uncomment this line
for (@files) {
next unless -f $dir . $_; # skip non-files
next unless /$criteria/; # skip files not matching RegEx
seek INFILE, 0, 0;
open OUTFILE, '>', $dir . $_ or die "Could not open '$_' for output
+: $!";
# binmode OUTFILE; # for non-text files uncomment this line
print "Copying '$_'...\n";
++$num;
print OUTFILE while (<INFILE>);
close OUTFILE;
}
close INFILE;
print "Done.\nCopied $num files.";
Hope this helped.
CombatSquirrel.
Entropy is the tendency of everything going to hell. | [reply] [d/l] |
Re: Newbie needs help replacing files in a directory
by Cody Pendant (Prior) on Oct 30, 2003 at 20:02 UTC
|
You really want to replace each and every file with the same file?
Then use opendir(), then readdir() and you'll have an array containing all the names of the files.
Then use File::Copy and go through that array (skip the files named "." and "..") and do
copy('path/to/replacement.htm','path/to/$_')
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print
| [reply] [d/l] [select] |
Re: Newbie needs help replacing files in a directory
by sauoq (Abbot) on Oct 30, 2003 at 19:30 UTC
|
You haven't explained how you want to map the "new .html files" to the old "original" names.
Can you give an example to go along with your description of the problem?
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
Sorry, I really don't understand what you mean by "Map".
I have an html file called new.html that exists in the directory.
I need that code to replace the code in every other html file in the same directory. This is a one time run.
I am really a newbie so excuse this mess but might help explain ...
#!usr/bin/perl
$newfile = newfile.html
$directory = /will/be/abs/path
open(dir,$directory);
$files=@file;
foreach $file {
if ($file =~ \.html) {
$filename = $file;
del($filename);
copy($newfile, newfile.dat); ????
rename(newfile.dat, $filename); ???
}
}
As you can see, I am pretty much clueless...
| [reply] |
|
|
#!/usr/bin/perl -w
use strict;
use File::Copy qw( cp );
opendir D, '.';
my @files = grep /\.html$/, readdir( D );
for ( @files ) {
next if $_ eq 'new.html';
cp $_, "$_.bak";
cp 'new.html', $_;
}
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] |
Re: Newbie needs help replacing files in a directory
by sgifford (Prior) on Oct 30, 2003 at 20:05 UTC
|
This program replaces all files in the current directory with the contents of the file in $SOURCEFILE
#!/usr/bin/perl -w
use strict;
use vars qw($SOURCEFILE);
$SOURCEFILE="/tmp/overwrite";
# First read in the source file
use vars qw($sourcefile);
open(F,"< $SOURCEFILE")
or die "Couldn't read '$SOURCEFILE': $!\n";
undef $/;
$sourcefile = <F>;
close(F)
or die "Couldn't close '$SOURCEFILE': $!\n";
# Now walk the directory and replace the files.
opendir(D,".")
or die "opendir error: $!\n";
while (my $f = readdir(D))
{
next if ($f =~ /^\./);
next unless (-f $f);
open(F,"> $f")
or die "Error overwriting '$f': $!\n";
print F $sourcefile;
close(F)
or die "Error closing '$f': $!\n";
}
closedir(D)
or die "closedir error: $!\n";
exit(0);
It's easier, though, and saves space if you just use links:
#!/usr/bin/perl -w
use strict;
use vars qw($SOURCEFILE);
$SOURCEFILE="/tmp/overwrite";
# Now walk the directory and replace the files.
opendir(D,".")
or die "opendir error: $!\n";
while (my $f = readdir(D))
{
next if ($f =~ /^\./);
next unless (-f $f);
unlink($f)
or die "Error unlinking '$f': $!\n";
link($SOURCEFILE,$f)
or die "Error linking '$f': $!\n";
}
closedir(D)
or warn "closedir error: $!\n";
exit(0);
| [reply] [d/l] [select] |
Re: Newbie needs help replacing files in a directory
by Art_XIV (Hermit) on Oct 30, 2003 at 20:07 UTC
|
Here's a sample of how you might do it on a win32 box. It's very brute-force but does the job. It assumes that the file that you will be replacing all of the others with is named 'replacer_file.html' and resides in the same directory as the files to be replaced.
use strict;
use File::Copy;
use constant REPLACEMENT_FILE => 'replacer_file.html';
use constant DIRECTORY => 'C:/foo/';
opendir(FOO, DIRECTORY);
my @file_list = grep /\.html?$/, readdir FOO;
for my $file (@file_list) {
unless ($file eq REPLACEMENT_FILE) {
unlink(DIRECTORY . $file); #remove original
copy DIRECTORY . REPLACEMENT_FILE, DIRECTORY . $file; #make copy o
+f replacer & rename to original
}
}
closedir FOO;
Hope this helps you along. One of the other monks may have a more elegant & idiomatic way of accomplishing this. | [reply] [d/l] |
Re: Newbie needs help replacing files in a directory
by mcogan1966 (Monk) on Oct 30, 2003 at 20:08 UTC
|
You might also want to consider the use of sudo. Once you have all your filenames, you can do something like:
system "sudo mv $newfile $oldfile";
Quick and easy, IMO. | [reply] [d/l] [select] |
|
|
WOW!
Thanks everyone!
You all are great!
I'll apply your knowledge accordingly!
Thanks again!
| [reply] |
Re: Newbie needs help replacing files in a directory
by Anonymous Monk on Oct 30, 2003 at 21:51 UTC
|
Thanks, this is the info that got me in the right direction and helped me the most!
#!/usr/bin/perl -w
#use strict; #had to remove
$SOURCEFILE="/abs/path/to/file/new.html";
# First read in the source file
use vars qw($sourcefile);
open(F,"< $SOURCEFILE")or die "Couldn't read '$SOURCEFILE': $!\n";
undef $/;
$sourcefile = <F>;
close(F) or die "Couldn't close '$SOURCEFILE': $!\n";
# Now walk the directory and replace the files.
opendir(D,".") or die "opendir error: $!\n";
while (my $f = readdir(D))
{
next if ($f =~ /^\./);
next unless (-f $f);
unless ($f =~ ".dat") {## I added this to ignore .dat files
open(F,"> $f")or die "Error overwriting '$f': $!\n";
print F $sourcefile;
close(F) or die "Error closing '$f': $!\n";
}
}
closedir(D) or die "closedir error: $!\n";
exit(0);
Thanks again... | [reply] |
Re: Newbie needs help replacing files in a directory
by Anonymous Monk on Oct 30, 2003 at 20:54 UTC
|
I guess I should have mentioned that I am attempting to do this on my web server account with netnation not locally.
I will keep trying with variations of your posts.
Thanks...
| [reply] |