Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Searching and Replacing file content within directory

by fisher (Priest)
on Nov 12, 2010 at 14:27 UTC ( [id://871063]=note: print w/replies, xml ) Need Help??


in reply to Searching and Replacing file content within directory

need a recursion? well then do it:
#!/usr/bin/env perl use warnings; use strict; my $curUrl = "http://localhost:8080"; my $tarUrl = "http://localhost"; my @buf; my ($line, $f); sub diveinto { print "dive into: $_[0]\n"; opendir DIR, $_[0] or die "Could not open directory"; my @files = readdir DIR; closedir DIR; foreach $f (@files) { if ($f =~ /\.{1,2}/) {next } print "processing: $f\n"; if ( -d "$_[0]/$f" ) { diveinto ("$_[0]/$f") } else { open FILE, "<". "$_[0]/$f" or die "Could not open file -- +$_[0]/$f"; @buf = <FILE>; close FILE; open W, ">" . "$_[0]/$f" or die "cunt open for r/w -- $_[0 +]/$f"; foreach $line (@buf) { $line =~ s/$curUrl/$tarUrl/g and print "Yes"; print W $line; } close W; } } } diveinto $ARGV[0];
* I leave debug 'print'-s as is to help you understand what it does. It can be shrinked twice in size.
* If it is a directory, we just call ourselves recursively
* The original loop where you trying to replace string need to write result of s///;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://871063]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found