Welcome to Perl and the Monastery, Apt_Addiction!

AFAIK, you should be able to rename files even if they're open on Mac OS X, although if you're going to do a mass rename in a directory tree, you probably don't want to be doing any other operations on that tree in the meantime, and therefore exit any programs that might be using it. What problems are you worried about here?

As for the code, a couple of notes: You don't check the return values of rename or open for errors, as in rename($_, $txt) or die "rename('$_','$txt'): $!"; and open(my $fh, '>>', $filename) or die "$filename: $!";. And if you're worried that other processes might be writing to the directory while you're doing your rename, note your code has a race condition: It's possible the file with the name $txt might be created in between the -e $txt and rename($_, $txt) (hence my above recommendation to try and ensure that your script is the only one working on the directory).

As for the dot files, I assume that's what you're trying to do with -f != m/^\./? Note that -f only checks if $_ is a regular file (or is a symbolic link that points to a regular file) and returns a true/false value, so doing an != on its return value doesn't really make sense. I guess you might have meant $_ !~ m/^\./, which evaluates to true if $_ doesn't match the regex - that's written more briefly as !/^\./.

Another improvement might be to not open your log file on every rename, but only open it once at the start of the program, that would be more efficient.

In general, you should Use strict and warnings, and you might be interested in Corion's Text::CleanFragment, which does a thorough cleanup of strings, although it might do too much for your purposes.


In reply to Re: Easy way to check if a file is open needed. by haukex
in thread Easy way to check if a file is open needed. by Apt_Addiction

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.