Exercise: Renaming Files En Masse

This exercise will provide another (small) tool for your toolkit. This utility allows you to rename files given a directory name, a pattern to look for, and a pattern to change it to. For example, if a directory contains the filenames Chapter_01.rtf, Chapter_02.rtf, Chapter_04.rtf, and so on, you could rename all the files to Hour_01.rtf, Hour_02.rtf, Hour_04.rtf, and so on. This task normally isn't easy at a command prompt and just silly when you're using a GUI-based file browser.

Using your text editor, type the program from Listing 10.3 and save it as Renamer. If you can, be sure to make the program executable according to the instructions you learned in Hour 1.

When you're done, try running the program by ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.