Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #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":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2021-02-26 00:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?