File Processing

Changing File Names

In this lesson the student will learn to:
  1. Open a directory.
  2. Search a directory.
  3. Change the names of files using the rename function
By the end of this lesson the student will be able to:
                                                                                
	Write a short Perl script which displays the
	files in a directory and allows the user to
	change the name of a file.
                                                                                

Changing File Names

the following example lists the contents of the current directory and alters the names of all the files contained in that directory which match a specified pattern by incrementing the numerical portion of their name by one.

#!/usr/bin/perl -w my @files = (); my $folder = "."; unless(opendir(FOLDER, $folder)){ print "Cannot open folder $folder!\n"; exit; } @files = readdir(FOLDER); closedir(FOLDER); foreach $item ( reverse sort @files){ #reversing the file names is important! if( $item =~ m/lib.*/ ){ $new_name = $item; chomp($new_name); $new_name =~ s/lib(\d\d).html/$1+1/ge; #increment name by one if($new_name<10) { $new_name = "lib0$new_name.html"; } else{ $new_name = "lib$new_name.html"; } print "$item --> $new_name\n"; #if file names not reversed the following line #will fail before reaching the rename function -e $new_name || rename($item,$new_name); } } exit;
Here's an example that changes dashes to underscores:
#!/usr/bin/perl -w my @files = (); my $folder = "."; unless(opendir(FOLDER, $folder)){ print "Cannot open folder $folder!\n"; exit; } @files = readdir(FOLDER); closedir(FOLDER); foreach $item ( sort @files){ if( $item =~ m/.*.png/ ) { $new_name = $item; chomp($new_name); $new_name =~ s/(.*)\-(.*)/$1_$2/g; print "$item --> $new_name\n"; #if file names not reversed the following line #will fail before reaching the rename function -e $new_name || rename($item,$new_name); } } exit;
Here's another example which changes uppercase letters to lowercase:
#!/usr/bin/perl -w my @files = (); my $folder = "."; unless(opendir(FOLDER, $folder)){ print "Cannot open folder $folder!\n"; exit; } @files = readdir(FOLDER); closedir(FOLDER); foreach $item ( sort @files){ if( $item =~ m/.*.png/ ) { $new_name = $item; chomp($new_name); $new_name =~ tr/A-Z/a-z/; print "$item --> $new_name\n"; rename($item,$new_name); } } exit;
Perform the following steps to properly test this script:

  • Create a test directory by typing mkdir TESTDIR and change directories into TESTDIR by typing cd TESTDIR.
  • Create several empty files using the touch command like this: touch lib01.html touch lib05.html touch lib09.html
  • Get the script up and running and run it several times. After each execution of the script check the names of the files by typing ls -l.


  • ASSIGNMENT:
    Write a script which does the following:
    1. Prints the names of the files in a directory
    2. Prompts the user to identify a filename to change by typing its name
    3. Prompts the user to enter a new name for the file
    4. Does not allow the user to change the name to a name which is already in the directory