Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: rename

by Aristotle (Chancellor)
on Sep 02, 2002 at 04:37 UTC ( #194523=note: print w/replies, xml ) Need Help??

in reply to rename

dir_list() is pretty useless; it takes a list and pushes it item by item onto an array. A simple array assignment will do that job just as well.

Not that there has to be one, because the @Dir_List array only gets used in one place. You can just as well use @ARGV itself there.

The [0000-9999] bit in your filename checking regex does not do what you think it does. Its result is a character class that will match a single digit from 0-9.

Also note that the filename may contain characters that have special meaning to the regex engine. In that case the regex will malfunction. You need to put \Q and \E around the $file_name.

Using an array, @files_good in your case, to check for existence, is very awkward and slow. Instead, put the values as keys into a hash, and checking for existance becomes a single operation rather than a loop.

A minor point is that you may want to include $! (or whichever other variable is appropriate) in your die message when dealing with filesystem operations. As a rule of thumb, each die message should contain at least one variable; that usually makes tracking down the source of the error a lot easier.

grep is essentially a loop; you don't need it if you're going to use a for loop to separate the resulting list into two arrays anyway.

Your code that checks for whether a filename already exists is defective; the new filename it checks for has no extension until after the test has succeeded.

Rather than starting a shell to call its ren command for every file, a very slow procedure, you can just use Perl's own rename function.

All that applied, the code looks much more manageably like this:

#!perl use strict; use warnings; use Win32::File; use Cwd; die "[usage message explaining the commandline parameters here]\n" unl +ess @ARGV; for my $dir (@ARGV) { print "\n\n$dir directory***********************\n\n"; my $filename_prefix = ucfirst $dir; my (@rename_file, %filename_seen); opendir(DIR, $dir) or die "unable to open directory: $!"; for (readdir DIR) { next unless -f "$dir/$_"; my $attribs; Win32::File::GetAttributes("$dir/$_",$attribs); next if $attribs & HIDDEN; if (/^\Q$filename_prefix\E(\d{4})/) { $file_seen{$_}++; } else { push (@rename_file, $_); } } closedir DIR; for (@rename_file) { my ($count, $newname); my ($extension) = /\.(gif|jpg|jpe|jpeg|bmp)\z/; do { $newname = $filename_prefix . sprintf("%04d", ++$count) . $extension } while exists $filename_seen{$newname}; print "renaming $item to $newname\n"; rename "$dir/$_", "$dir/$newname"; } }

Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://194523]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-02-26 01:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found