Here is the fixed copy with some print()'s in it. I tested this copy so it should work. There were a few problems with the code. The grep was missing a terminating parenthesis. It (the script) was also missing a few other things. Hope this helps.

#!/usr/local/bin/perl -w # written By Jim Conner 4-27-2001 use strict; use vars qw(@FILES); use Cwd; $| = 1; my $changed = "0"; my $fcount = "0"; my $lcount = "0"; # open our current directory opendir(DD,".") or die("Cannot open ". getcwd ."\n"); # read our files getting only files named *.html # and *.asp @FILES = grep(/(\.html|\.asp)/,readdir(DD)) or die ("Could not get dir +ectory listing: $!\n"); if ( $#FILES == 0 ) { print "Sorry, there were no files in ". cwd ." that matched *.html + or *.asp\n"; exit(1); } else { print "There are $#FILES files in ". cwd ." that match *.html or * +.asp\n"; for ( @FILES ) { print "$_\n";; } print "Processing continuing...\n\n"; } # close our directory descriptor close(DD); # walk our array that has the file names in it we should go through. for ( @FILES ) { my $file = $_ ; print "Processing file: $file\n"; my $newfile = "$file-fixed"; print "Output file will be: $newfile\n"; # open two descriptors # FD for our original files open(FD,$file) or die("Cannot open $file: $!\n"); # NEW for our newly created (hopefully fixed) files. open(NEW,">>$newfile") or die ("Cannot open $newfile for writing: +$!\n"); # walk each file while ( <FD> ) { my $line = $_ ; chomp $line; # replace anything in a line with <a href in it # that has .html to .asp if ( grep(/a href.*\.html/,$line) ) { #print "Found a match -> "; #print "$line"; #print " <-...converting\n"; ( my $newline = $line ) =~ s/\.html/\.asp/g; #print "Substitution written was: [[ "; #print "$newline"; #print " ]]\n"; $changed = "1"; $lcount++; # append that to the new file. print NEW "$newline\n"; } else { # otherwise just append the old line into # the file. print NEW "$_\n"; } } # Close the descriptor for these files before # the next loop iteration close(NEW); close(FD); print "Finished with $file...\n\n"; if ( $changed == "1" ) { $fcount++; $changed = "0"; } } print "There were $#FILES total files\n"; print "There were $fcount files affected\n"; print "There were $lcount lines changed\n"; print "Have a nice day :) \n\n";

Bear in mind that this only matches "a href" in lower case. If you have some address hypertext references in caps, this won't catch them without some tweaking.

----------
- Jim


In reply to Re: Re: Re: Changing .html to .asp by snafu
in thread Changing .html to .asp by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.