检测文件正在复制到文件夹中 [英] Detecting that files are being copied in a folder
问题描述
我正在运行一个脚本,该脚本将从特定位置复制一个文件夹(如果该文件夹不存在(或不一致)).当我同时运行脚本2次以上时,会出现问题.当第一个脚本试图复制文件时,第二个脚本来尝试相同的东西,导致混乱.我如何避免这种情况?类似于系统范围的互斥锁.
I am running a script which copies one folder from a specific location if it does not exist( or is not consistent). The problems appears when I run concurently the script 2+ times. As the first script is trying to copy the files, the second comes and tryes the same thing resulting in a mess. How could I avoid this situation? Something like system wide mutex.
我尝试使用-w
进行简单测试,我手动复制了该文件夹,并且在复制文件夹时,我运行了脚本:
I tryed a simple test with -w
, I manually copied the folder and while the folder was copying I run the script:
use strict;
use warnings;
my $filename = 'd:\\folder_to_copy';
if (-w $filename) {
print "i can write to the file\n";
} else {
print "yikes, i can't write to the file!\n";
}
这当然是行不通的,因为我仍然对该文件夹具有写入权限.
关于如何检查文件夹是在Perl
中还是在使用batch commands
复制的任何想法?
Of course this won't work, cuz I still have write acces to that folder.
Any ideea of how could I check if the folder is being copied in Perl
or usingbatch commands
?
推荐答案
听起来像的工作锁定文件.有许多实现锁定文件的CPAN模块,但其中大多数在Windows上不起作用.根据CPAN测试人员,以下一些似乎支持Windows:
Sounds like a job for a lock file. There are myriads of CPAN modules that implement lock files, but most of them don't work on Windows. Here are a few that seem to support Windows according to CPAN Testers:
File::LockfileFile::TinyLock- File::Flock::Tiny
快速查看源代码后,我推荐的唯一模块是 File :: Flock :: Tiny .其他人似乎很暴躁.
After having a quick view at the source code, the only module I can recommend is File::Flock::Tiny. The others seem racy.
这篇关于检测文件正在复制到文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!