Glad you could stop by the Linux Basement site. Linux Basement is an informational Podcast about Linux, open source software and lots of other wonderful technology. If you want to find out more about open source technologies, subscribe and have a listen!

#linuxbasement is up at irc.freenode.net

MP3 Feed
Ogg Vorbis Feed
MP3 Feed (all episodes)
Ogg Feed (all episodes)

 

Perl script to rename web files and links

Article Type: 
Other

I posted this here http://thelinuxsociety.org.uk/content/perl-script-to-rename-web-files-an... a while back but I thought I'd copy it here.

This is a little Perl script I had to write for a piece of coursework I've posted the code below and attached the file with an extra .txt extension(http://thelinuxsociety.org.uk/files/script.pl.txt), its far from the best coding but it should:

Assist in the maintenance of a web site by allowing the HTML file for a web page to be renamed and automatically update the links in any other pages to refer to the new name, only for a single directory however as recursion is not enabled.

Its unlikely that anyone in my class is seeing this but if you are, its not a good idea to copy because this lecturer, unlike the others will notice and because I want to share not cheapen education.

If said lecturer is seeing this and would like me to take the script down please contact me.

Source:

 <code>
#!/usr/bin/env perl

# AUTHOR: Kris Davidson
# DATE:   March 2008

# FUNCTION:
# This script should allow an HTML file to be renamed then loop through all other files in the directory
# and update the links in those files to refer to the new name.
# It expects two alphanumeric parameters both ending .html and seperated by a space.

# CONVENTIONS:
# 1. Spaces instead of tabs
# 2. Bracketing BSD and GNU
# 3. Identation is 8 columns

# TODO:
# 1. Allow directory recursion?
# 2. Code to be cleaned up / improved

# MISC:
# A note to the Perl-One-Liners, yes I'm aware that a chunk of the script could be reduced to something
# perl -i -pe 'BEGIN { $from = shift @ARGV; $to = shift @ARGV; } s/$from/$to/g'
# You may value obfuscation, I however do not.

#
# Pragmas/Best practice options
#
use warnings;
use strict;

#
# Definitions (A bit C like I know, but I think it keeps the code readable and tidy)
#
my $changed;        # A Security/safety consideration that indicates a files content has changed
my $correct;        # Holds argument validation
my $fileHandler;    # Holds the file currently being read/searched/changed
my $newName;        # The new file name
my $oldName;        # The old file name
my $pool;           # All files to be searched and updated
my $usage;          # Holds correct usage statement
my @newFile;        # Array that contains changes made

#
# Input validation
#
$usage =
"
Syntax: [Old file name] [New file name]
        Seperated by a single space with the .html extension included.
\n";

#
# Each argument must end with .html
#
if (defined $ARGV[0] && $ARGV[1])
{
        $correct = ($ARGV[0]=~ /.+\.html$/) & ($ARGV[1]=~ /.+\.html$/);
}

die $usage unless $correct;

#
# Argument passing
#
$oldName = $ARGV[0];
$newName = $ARGV[1];

#
# Some validation and error checking
#
if ($oldName eq $newName)
        {
                die "\n [Old file name] and [New file name] are the same, no changes made\n\n";
        }
elsif (! -e $oldName)
        {
                die "\n [Old file name] does not exist, aborting\n\n";
        }
elsif (-e $newName)
        {
                die "\n [New file name] already exists, aborting\n\n";
        }

#
# Rename file
#
rename ($oldName, $newName) or die "ERROR, could not rename file: $!\n";

while ($pool = <*html>)                                       # REMOVED for formatting, check attached
{
        open (FILE, "<", $pool) or die "$0: $pool: $!\n";     # REMOVED for formatting, check attached

        while ($fileHandler = <FILE>)                         # REMOVED for formatting, check attached
        {
                if ($fileHandler =~ s/$oldName/$newName/g)    # REMOVED for formatting, check attached
                {
                       $changed = 1;
                }
                push (@newFile, $fileHandler);                # Add changes to array
        }   
    close (FILE);
        if ($changed)
        {
                open (NFILE, ">", $pool);    # Open file for writing / replacing
                print (NFILE @newFile);      # Write the constructed array to the file
                close (NFILE);               # Close file / clean-up
                $changed = 0;
        }
        undef @newFile;                      # Erase the array before moving to the next file
}
</code>