Using Perl to Convert Hyperlinks and Filenames to Lowercase

0
22

Like a lot of web developers, I’m not always that disciplined when it comes to the file naming convention I use and I sometimes end up with a situation whereby I have some files that are in lowercase, some that begin with a capital, and some that are a bit of a mixture.

One web site I maintain contains about 2000 web pages and has about 20,000 hyperlinks. As you can imagine, I had one of those sinking feelings when I was told that in order to migrate the web site to a new content management system, all the file names and hyperlinks would need to be changed to lowercase.

Whenever I am presented with a problem like this, my instinct is always to write a Perl script using one or more regular expressions to solve the problem. This particular situation was no exception.

Change a string to lowercase

The following regular expression changes all the characters in a string to lowercase. The first part of the regular expression finds a hyperlink, and the second part converts the string. (Just in case this article is not displayed correctly, there should be a single backslash in front of the ‘L $ 1’).

Change a filename to lowercase

Likewise, changing a filename itself is very simple. The following two lines perform the task quite nicely:

(Again, there should be a single backslash in front of the ‘L $ name’.)

If you need more information on how to incorporate the above code snippets into a complete script, feel free to contact me directly.

Leave a Reply