Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: rename by Aristotle
in thread rename by jjdraco

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found