#!perl
# rename.pl [dir1]...[dir##]
# takes a list of directorys as parameters
# renames all files in given directory to dir####
# not sure if it will run on other OS besides Win32
# by Joseph Jones <jjdraco@acsalaska.net>
use strict;
use warnings;
use Win32::File;
use Cwd;
#
# global Variables
#
my @Dir_List;
#
# Subroutines
#
sub dir_list
{
#
# gets the list of directories from @ARGV
# stores directory list in @Dir_List
# need to put in error handling to check for invalid directory name
+s
#
my $count = @_; # gets length of @ARGV
if ($count == 0) { die "No arguments where passed"; }
# dies if no args where passed
foreach my $item (@_) #pushes list of directorys on to array
{
push (@Dir_List, $item);
}
} # end of sub dir_list
sub scan_dir
{
# gets passes a name of a directory
# goes through all files in that directory and renames them
my (@file_list, @files_good, @rename_files, $file_name, $dir, $attr
+ibs);
$file_name = $_[0]; #name of directoyr will be name
+ of file
$file_name = ucfirst $file_name; #make sure $file_name is capita
+lized
#gets current directory and changes to new directory
$dir = cwd();
$dir .= '/' . $file_name;
chdir($dir);
opendir(DIR, $dir) or die "unable to open directory";
@file_list = grep { # filters out hidden files and directories
# stores directory list in @file_list
-f $dir . '/' . $_
&& Win32::File::GetAttributes($_, $attribs)
&& !($attribs & HIDDEN)
} readdir(DIR);
foreach
my $item (@file_list) # find out which files have already been
+ renamed
{
if ($item =~ /^$file_name[0000-9999]/)
{ #list of files that have been renambed
push (@files_good, $item);
} else
{ #list of files that need to be renamed
push (@rename_files, $item);
}
}
@files_good = sort @files_good;
foreach
my $item (@rename_files) # goes through list of files and rena
+mes them
{
my ($newname, $count);
$count = "0001"; # appended to end of $file_nam
+e
$newname = $file_name . $count;
foreach
my $item2 (@files_good) # checks to see if $file_name.$coun
+t already
{ # exists in @files_good
++$count
if ($item2 =~ /$newname/); # if file exist goes to next
+possible
$newname = $file_name . $count;
} # file name $file_name.$count+1
$item =~ /(gif|jpg|jpe|jpeg|bmp)$/; # finds the file extentio
+n
$newname = $newname . "\." . $1; # and appends it to $newn
+ame
push (@files_good, $newname); # pushes $newname on to list so
+ when
# it looks for next availible n
+ame
# it knows this name is already
+ taken
@files_good = sort @files_good;
print "renaming $item to $newname\n";
system("ren \"$item\" \"$newname\"");
}
closedir(DIR);
chdir("..");
}
&dir_list(@ARGV);
foreach my $item (@Dir_List)
{
print "\n\n$item directory***********************\n\n";
&scan_dir($item);
}
In reply to rename
by jjdraco
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.