License Public Domain
Lines 29
Keywords
file management (6)
Permissions
Owner: Jesse I.
Viewable by Everyone
Editable by Jesse I.
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

Recursive Filename Change Atom Feed 0

In Brief The perl program namechange.pl was created to recursively change the file extensions of any file. The command line parameters are the type you are changing from and the type you are changing to. Then as an input, you type the name of whatever directory you would like to run the program on.... more
# 's
 1#!/usr/bin/perl
2use strict;
3
4#This program changes the file extension of any type of files-it takes 2 commands in the command line
5#Command lines 2 arguments are the old and new file extensions
6
7
8my $directory;
9my $curExt = "." . @ARGV[0];
10my $newExt = "." . @ARGV[1];
11my $dir=<STDIN>;
12chomp $dir;
13
14if($dir =~ /((.|\s)+)\//){
15 $dir = $1;
16}
17
18renameHtmlToPhp($dir, $curExt, $newExt);
19
20use File::Glob qw(bsd_glob);
21
22sub renameHtmlToPhp{
23 my($directory, $curExt, $newExt) = @_;
24 my @items= bsd_glob($directory . "/*");
25 foreach my $file (@items){
26 if(-f $file){
27 if($file =~ /((.|\s)+)$curExt$/){
28 my $newname = $1 . $newExt;
29 rename $file, $newname;
30 }
31 }
32 else{
33 renameHtmlToPhp($file, $curExt, $newExt);
34 }
35 }
36}

The perl program namechange.pl was created to recursively change the file extensions of any file. The command line parameters are the type you are changing from and the type you are changing to. Then as an input, you type the name of whatever directory you would like to run the program on.

Ex. jesse% perl namechange.pl html php

/Users/jesse/Desktop/newsletters/

IMPORTANT: File types should NOT be put in with the periods (Ex. DONT DO THIS ) jesse% perl namechange.pl .html .php

/Users/jesse/Desktop/newsletters/