web blazonry web development scripts and tutorials Get best price on Samsung Galaxy S5, Copper Gold 16GB (Verizon Wireless)
   PHP       Name Generators       Perl       CSS       Javascript       Java       MySql       How Tos       Resources   

Perl Home

Perl Scripts
  Count Links
  Mail to MySQL
  ProcLog Files
  Search and Replace
  Site Monitor
  UnTabify

Perl Articles
  csv2htm Converter
  OLE Automation
  Regular Expressions
  SubDir Recursion

Perl Resources
  Regular Expressions Quick Reference
  Regular Expressions Examples
  Recommended Sites
  Bestselling Perl Books
  Free Perl Books (new)
  Free Python Books(new)

Bookmark and Share




Perl : SubDir Recursion

A great use of Perl is for small utility scripts. Two utility scripts that I have written are a LowerCase script, which renames all the files in a directory and its subdirectories to all lowercase, and an Include script, which recurses through all HTML files in a directory searching for an include tag and inserts the appropiate files.

Both of these are used to maintain this or other web sites, and both of these scripts use a very handy Perl module called File::Recurse. The File::Recurse module was written by Aaron Sherman and can be download at any CPAN site.

The module is bundled with the File-Tools-2.0 module which can be downloaded from the CPAN site here (/modules/by-module/File/). The module works on both UNIX and Win32 systems running Perl.

The module allows you to perform a function on all files/dir inside of a specified directory. It recurses through all the subdirectories automatically performing the specified function on each file.

Here an example program which will lowercase all files in a specified directory. It reads in the directory through the command-line when you execute the script.

use File::Recurse;

# read in directory
$dir = $ARGV[0];

# verify directory
if (-d $dir) {
  print "Working Directory: $dir\n";

} else { print "\nError: Directory Not Found $dir\n"; exit(0); }


# call recurse function from module
# this applies the sub-routine lcName to all files
# in the specified directory

recurse(\&lcName, $dir);


# define the lcName sub-routine which lowercases all files


sub lcName {
  # receive passed filename
  $filename = $_[0];

  $newfilename = $filename;

  # regexp that converts to lowercase
  $newfilename =~ tr/A-Z/a-z/;

  # if I can read and write to it
  # rename the filename to the lowercase one

  if ((-r $filename) && (-w $filename)) {
     rename($filename, $newfilename);
     print " Renamed: $filename to $newfilename\n";
  }

}


Related Links:

File::Recurse Documentation

Perl.com

Perl Documentation On-Line

Listing of CPAN sites

 

Newest Pages
Test Google Ads Ver. 2
Free Linux Admin Books
Free Linux Books for Programmers
Free Books for Linux on the Desktop
Free PHP Books
Free JavaScript Books
Free Java Books - Advanced
Free Java Books - Basic
Free Perl Books
Free Python Books
Quote of the Day (PHP)
Debugging Part 2
How to Test Google Ads
Most Popular Pages
Baby Name Generator
U.S. Name Generator
Wu Name Generator
Popup Windows (JavaScript)
Upload and Resize an Image (PHP)
How To Install Apache + PHP + MySQL
Intro to Web Databases (PHP, MySQL)

Least Popular Pages
iNews Applet (Java)
Java Resources
Site Monitor (Perl)
PHP Resources
 
 

  privacy policy     ||     © 1997-2016. astonishinc.com   All Rights Reserved.