extensionChanger.pl
This script will rename all files with one extension to another extension. Example: all files in a given directory that end with .php will be renamed to end with .txt.
The only change you might have to make is if you’re on a Linux machine and Perl is not at /usr/bin/perl, but somewhere else, in which case you would need to change the first line to contain the proper path. I’ve not tested this program on a Windows box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#!/usr/bin/perl # Renames file extensions # # Syntax: # perl extensionChanger.pl --from=FIRST_EXTENSION --to=SECOND_EXTENSION # Example use: # perl extensionChanger.pl --from=html --to=php # Optional: # Specify a directory to work in. Defaults to PWD. # --dir=DIRECTORY # Example use: # perl extensionChanger.pl --from=exe --to=pl --dir=tmp # # Sarah; 2005-12-18 use Getopt::Long; GetOptions( "from=s" => \$ext1, "to=s" => \$ext2, "dir=s" => \$dir ); if ($ext1 eq "" or $ext2 eq "") { print "Syntax:\n"; print "\tperl extensionChanger.pl --from=FIRST_EXTENSION --to=SECOND_EXTENSION\n\n"; print "Example:\n"; print "\tperl extensionChanger.pl --from=txt --to=php\n\n"; print "Optional:\n"; print "\tSpecify a directory to work in. Defaults to PWD.\n"; print "\t--dir=DIRECTORY\n\n"; print "Example:\n"; print "\tperl extensionChanger.pl --from=exe --to=pl --dir=tmp\n"; exit; } else { if ($dir eq "") { $dir = "."; } opendir(DIR, $dir) || die "Could not open directory for reading."; # Get an array of all files with the old extension that are in the given directory @files = grep(/\.$ext1$/, readdir(DIR)); if ($files[0] eq "") { print "There are no files of extension '$ext1' "; if ($dir eq ".") { print "in PWD"; } else { print "in $dir"; } print ".\n"; } else { # For every file with the old extension in the given directory... foreach $file (@files) { print "$file "; if ($dir ne ".") { print "in $dir/ "; } # Remove the current extension from the $file variable $file =~ s/\.$ext1//g; # Rename the file to have the new extension $error = `mv $dir/$file.$ext1 $dir/$file.$ext2`; print "is now $file.$ext2\n"; # If there were errors, print them if ($error ne "") { print "E: $error\n"; } } } closedir(DIR); } |
comments powered by Disqus