I'm writing a script to run on macOS that will rename all illegal files in a tree, so they can go to OneDrive and Dropbox without jamming the processes.
I'm new to Perl and only a beginner hobby coder, dipping my toes into Perl, but I've learned a lot and asked a lot and have come up with this, which does everything but two things - check if a file is already open before renaming it. and avoid dot files.
I'm aware of lsof but I'm wondering if there's an easier and quicker way to check if any process in the system has a handle on a file open?
use warnings; use File::Find; use 5.010; use POSIX; find(\&dirRecurs, '/users/my-dir/tree-to-work-on'); sub dirRecurs{ if ((my $txt = $_) =~ s/^\ | (?=\.)|[\/!#%^:<>?*&()\\]| $//g & +& -f != m/^\./) { my $filename = '/users/my-dir/fix_names_report.txt'; open(my $fh, '>>', $filename); if (! -e $txt){ rename($_, $txt); say $fh (strftime "%Y-%m-%d %H:%M:%S", localtime t +ime)," \: ", $_, " => ", $txt; } else { say $fh ("!!! ", strftime "%Y-%m-%d %H:%M:%S", loc +altime time)," \: ", $_, " => NOT RENAMED =>", $txt, " already exists +."; } close $fh; } }
Any advice or suggestions?
In reply to Easy way to check if a file is open needed. by Apt_Addiction
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |