#!/usr/bin/perl # http://herselfswebtools.com # Linda MacPhee-Cobb # This program will add in the Google Analytics code to every file in THIS directory # that has in it. # # if you used or or something else be sure to change # the line below $oldString = "" to match the case of the body tag used in your files # # YOU MUST change the _uacct = \"ZZ-9999999-9" to your Google analytics account number # this is a dummy number. See the line that starts $newString it's in there # # Creative Commons Attribution 3.0 License # # no warranties are given or implied. # use at your own risk # preferably on a copy not your original files. # # # Put this file in the same directory as the files you wish to change # files in this directory will be re-written. The delete or move this # file out of that directory when you are finished # # # use current directory # $directory = "."; chomp($directory); # # out with the old # $oldString = ""; chomp($oldString); # # In with the new # Change the _uacct to be your Google Analytics account number!!!! # $newString = "\n\n\n"; chomp($newString); # # last chance to bail before changing files # print "\nDid you change the uacct number? If not bail now!!! y or n: "; $answer = ; chomp($answer); if( $answer eq "y" ) { print( "Altering files now...\n" ); } else { die ( "No changes were made, try again!\n" ); } # # read current working directory and store file names in @flist # opendir( INDIR, "$directory") || die ("$0: Can not read directory"); while( $fname = readdir INDIR){ next if $fname =~ /^\./; # not hidden next if $fname =~ /^\.\.?$/; # it's pwd or parent next if $fname =~ "google-code.pl"; # don't change this file push @flist, $fname; } closedir(INDIR); # # read each file and switch strings if needed # write to a file named temp in current dir # if changed any lines, let user know and copy # temp to correct file name in current directory # foreach $fn (@flist) { open( IN, "$directory/$fn") || die ("\n can not read $fn \n"); open( OUT, ">temp") || die ("\n can not write $fn \n"); $flag = 0; $lineNo = 0; while( ){ $lineNo++; $line = $_; if ( $line =~ m/$oldString/ ) { $flag = 1; } $line =~ s/$oldString/$newString/ && print "\n Changed: $fn \n"; print OUT "$line"; } close(IN); close(OUT); if($flag){ rename (temp, $fn) || die "couldn't rename temp to $fn.\n"; } }