Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

File Find

by oaklander (Acolyte)
on Jan 15, 2002 at 19:34 UTC ( [id://138922]=perlquestion: print w/replies, xml ) Need Help??

oaklander has asked for the wisdom of the Perl Monks concerning the following question:

I have this find script that only seems to work 1 out of 10 times. Most of the time it corrupts the files and messes up the data. It is supposed to change a word in a directory and ALL of its sub directories on an NT. Can someone advise what I am doing wrong here to make this work correctly??
use File::Find; my @dirs = qw( . ); find ( \&change, @dirs ); sub change { if ( -f && /\.html$/ ) { my $file = $_; open(IN, $file) or die "CANT OPEN FILE!\n"; while(<IN>) { $data .= $_; } close IN; $buffer =~ s/OLDWORD/NEWWORD/gis; open(OPF,">$file") or die "NOT OPENING FILE FOR MOD, $!\n"; print OPF "$data"; close OPF; } }

Replies are listed 'Best First'.
Re: File Find
by busunsl (Vicar) on Jan 15, 2002 at 19:46 UTC
    Parameters for subroutines are stored in @_.

    You should place a $_ = shift; at the beginning of change.

    Or even better:

    sub change { my $file = shift; if (-f $file && $file =~ /\.html$/) { . . .
Re: (Buzzcutbuddha - Some Corrections) - File Find
by buzzcutbuddha (Chaplain) on Jan 15, 2002 at 20:14 UTC
    A couple of pointers:

    • Instead of using the $_ variable for the name, I would suggest using $File::Find::name because that will more likely hold the correct filename you want.
    • Maybe I'm not understanding something but you're making your global changes to $buffer, but I only see it used once. Did you mean to make your change to $data?
    • Since you want to loop through many files and directories, you should perhaps change your dies to warns so the script continues, and you can hand modify the files.
    • Doing global changes will probably be faster if you slurp the whole file into memory all at once:
      { local $/ = undef; $buffer = <IN>; }
    • Regardless of whether or not the file contains OLDWORD, you're opening the file and rewriting it (which is where your corruption is probably coming from). You should only rewrite the file if you made any changes to it. The s/// regex returns the number of changes made, so as long as catch that change, you can test on the value later.
    • And according to the mantra here, always always
      use strict; use warnings;

    Here's a quick untested rewrite I did to highlight the points I made.

    use strict; use warnings; use File::Find; my @dirs = qw( . ); find ( { wanted => \&change, no_chdir => 1}, @dirs ); sub change { my $file = $File::Find::name; if ( -f $file && $file =~ /\.html$/ ) { open(IN, $file) or warn "CANT OPEN FILE!\n"; my $buffer; { local $/ = undef; $buffer = (<IN>); } close IN; my $changecount = ($buffer =~ s/OLDWORD/NEWWORD/gi); if ($changecount) { open(OPF,">$file") or warn "NOT OPENING FILE FOR MOD, $!\n +"; print OPF "$buffer"; close OPF; } } }

    This could be optimized and shrunk I know, but I'll leave that to the gurus. I hope this helps.
    Take care,
    bcb

    EDITED: Per a notice from Oaklander, the script would enter directories, but was not changing any of the files. A quick modification changed the call to File::Find from find ( \&change, @dirs ); to find ( { wanted => \&change, no_chdir => 1}, @dirs );.

    I tested the code and it works as Oaklander needs.

      I tried your script and it only changed the data in the main directory. It didnt make any changes to the sub directories. Please advise on how I can get this to work?
      I get an error message with this attempt saying it doesnt like the 'use warnings;' on line 2. Please advise if I need to add a 'warnings' module? Here is the error message:
      BEGIN failed--compilation aborted at C:\Perl\bin\find9.pl line 2.

      Script:
      use strict; use warnings; use File::Find; my @dirs = qw( . ); find ( { wanted => \&change, no_chdir => 1}, @dirs ); sub change { my $file = $File::Find::name; if ( -f $file && $file =~ /\.html$/ ) { open(IN, $file) or warn "CANT OPEN FILE!\n"; my $buffer; { local $/ = undef; $buffer = (<IN>); } close IN; my $changecount = ($buffer =~ s/FLORIDA/NEWWORD/gi); if ($changecount) { open(OPF,">$file") or warn "NOT OPENING FILE FOR M +OD, $!\n+"; print OPF "$buffer"; close OPF; } } }

      I tried running this without the 'use warnings;' part and it only changed words in the main directory so I assume I need this part to work so it changes the sub directories as well?

        hmmmmm. this is certainly a stubborn little script we're dealing with. alright.

        About Warnings
        use warnings; is equivalent to putting the little -w at the end of your 'shebang line'. So if your Perl implementation does not grok use warnings' change your script to read #!/usr/bin/perl -w at the top of your code. This will have the same effect. Warnings have the effect of telling Perl you want it to complain about any code that's incorrect or not clear. An example would be (at least until Perl 6 comes out):

        Without warnings, Perl will compile and run the following code

        # this is a slice, probably not intended @someList[0] = 'foo';
        but with warnings, if you tried to use the above code, Perl will complain:
        @someList[0] better written as $someList[0]
        which is what you want. Using warnings (either through 'use warnings' or the '-w' flag is one of the most important steps you can take towards being a good Perl programmer.

        Back To the Problem At Hand
        That all being said, 'use warnings;' is not needed to make this script run correctly.

        Some Questions

        • What version of Perl are you using, and on which platform?
        • How big are the directories that you're trying to parse?
        • Are the directories local or are they network shares?
        And have you tried adding some debugging to the script to see what it's doing? Maybe you want to do something like:
        #!/usr/bin/perl -w #alternative warning syntax use strict; use File::Find; ######################################################### # a debugging variable ######################################################### my $totalChanges; my @dirs = qw(.); find ( { wanted => \&change, no_chdir => 1}, @dirs ); sub change { my $file = $File::Find::name; if ( -f $file && $file =~ /\.html$/ ) { ################################### # debugging print statement ################################### print "going into $file\n"; my $buffer; open(IN, $file) or warn "CAN'T OPEN $file!\n"; { local $/ = undef; $buffer = (<IN>); } close IN; my $changecount = ($buffer =~ s/FOO/BAR/gi); if ($changecount) { ####################################### # increment debug variable ####################################### $totalChanges++; ####################################### # debugging statement ####################################### print "changed $changecount instances in $file\n"; open(OPF,">$file") or warn "NOT OPENING $file FOR MOD, $!\ +n"; print OPF "$buffer"; close OPF; } } } ################################################### # debugging statement ################################################### print "$totalChanges files changed\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found