Apt_Addiction has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Easy way to check if a file is open needed.
by haukex (Archbishop) on Apr 08, 2019 at 19:09 UTC | |
|
Re: Easy way to check if a file is open needed.
by choroba (Cardinal) on Apr 08, 2019 at 19:07 UTC | |
by Discipulus (Canon) on Apr 09, 2019 at 07:06 UTC | |
by choroba (Cardinal) on Apr 09, 2019 at 07:35 UTC | |
by Apt_Addiction (Novice) on Apr 09, 2019 at 09:22 UTC | |
|
Re: Easy way to check if a file is open needed.
by Apt_Addiction (Novice) on Apr 08, 2019 at 20:01 UTC | |
by haukex (Archbishop) on Apr 08, 2019 at 20:18 UTC | |
by Apt_Addiction (Novice) on Apr 08, 2019 at 23:21 UTC | |
by haukex (Archbishop) on Apr 09, 2019 at 08:17 UTC |