This can be pretty dangerous, there is no check wether the target filename already exists. For example, imagine you want to convert filenames to uppercase, but there is already an uppercase filename you unfortunately overlooked:
ls -1

a
b
c
A
The script would rename 'a' to 'A' and the former contents of 'A' would be lost.

For more fun, it is even possible to accidentally rename a file early in the list into a filename that is present later in the list, e.g. if you want to add 9 to numbers present in photo filenames:

ls -1
photo-1
photo-2
photo-3
photo-4
photo-5
photo-6
photo-7
photo-8
photo-9
photo-10
should become
photo-10
photo-11
photo-12
photo-13
photo-14
photo-15
photo-16
photo-17
photo-18
photo-19
The regexp would look somewhat like s/(\d+)/sprintf('%d', $1 + 9)/e (untested...) The loop first turns photo-1 to photo-10 (clobbering the original photo-10), later the clobbered photo-10 will become photo-19. The old photo-10 is gone, a new photo-10 is nowhere to be found and the new photo-19 is equal to the old photo-1. Confusion!

Maybe it is better to build a list of filename replacements first and check this list for these issues before doing any permanent change.


In reply to Re: Yet another perl-rename tool by chb
in thread Yet another perl-rename tool by Tanktalus

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, 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:
    & &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.