Category: | miscellaneous |
Author/Contact Info | Joseph Jones jjdraco@acsalaska.net |
Description: | will take a list of directory names that are subdirectories to the location of rename.pl and rename all image files in thoses directories to directoryname####.ext this is my first program written in perl, don't know if any one would have a use for it but i would like some feedback. |
#!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); } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: rename
by Aristotle (Chancellor) on Sep 02, 2002 at 04:37 UTC | |
Re: rename
by sauoq (Abbot) on Sep 02, 2002 at 20:55 UTC |
Back to
Code Catacombs