#!/usr/bin/perl -w # Date : 2-25-2000 # Author : James Dean Palmer # # Description : # # This script walks a directory tree performing the "process" function # on any html, htm, shtml, and txt files. # # The process function in this case looks for a telephone number of # the format 409-845-xxxx, or something similar. It also warns of # other 409 numbers it doesn't identify. # # You should run it like this: # # jphone.pl > log sub process { my ($file) = $_[0]; my ($prefix) = $_[1]; my ($thefile) = ""; if (!open (THEFILE, "< $file")) { print "ERROR: Could not open the file for reading.\n"; return; } while () { $thefile .= $_; } close (THEFILE); # The magical regular expression of doom: if ($thefile =~ m/409\D{0,3}\d(?!45)/) { print $prefix . "WARNING: This file contains other 409 numbers.\n"; } if ($thefile =~ m/409\D{0,3}845\D{0,3}\d{4}/) { $thefile =~ s/409(\D{0,3}845\D{0,3}\d{4})/979$1/g; if (-w $file) { if (!open (THEFILE, "> $file")) { print $prefix . "ERROR: Could not open file for modification (2)."; return; } print THEFILE $thefile; close (THEFILE); print $prefix . "STATUS: Modified file.\n"; } else { print $prefix . "ERROR: Could not open file for modification (1).\n"; } } } sub walk { my ($thisdir) = $_[0]; my ($depth) = $_[1]; my ($prefix) = ""; my (@files) = (); my ($file) = ""; my (@dirs) = (); my ($dir) = ""; for (my $i=0; $i<$depth; $i++) { $prefix .= " ";} if (chdir $thisdir) { print $prefix . "Changing to directory \"$thisdir\"\n"; } else { print $prefix . "ERROR: Could not change to directory \"$thisdir\"\n"; return; } opendir THISDIR, "."; # Ignore dir . and .. readdir THISDIR; readdir THISDIR; @files = readdir THISDIR; closedir THISDIR; foreach $file (@files) { if ((-e $file) && (-d $file) && (!-l $file)) { &walk ($file, $depth+1); if (chdir "..") { print $prefix . "Returning to \"$thisdir\"\n"; } } else { $_ = $file; if ((m/\.shtml$/i) || (m/\.html$/i) || (m/\.htm$/i) || (m/\.txt$/i)) { print $prefix . "Examining \"$file\"\n"; &process ($file, $prefix); } } } } # By default walk the current directory, I guess we could get fancy # and take arguments.. &walk (".", 0);