在 Perl 和 Windows 中找出文件是否可写/可移动

Find out in Perl and Windows if a file is writeable/ removable

我想在 Windows 下为 Hot-Folder 在 Perl 中构建一个 Watch-Dog(我可能称它为 Folder-Watch 或者,嗯,可能更好:Hot-Dog)。 到目前为止,我成功地做到了这一点,Win32::ChangeNotify(见下面的示例)。

但是你可能会猜到阅读源代码我 运行 遇到了一个问题,当 $watchdir 中文件的 copying/creating 进程尚未完成时移动进程想要完成(没有这样的文件或目录)。

use Win32::ChangeNotifier;
use File::Copy qw(move);

my $notify = Win32::ChangeNotify->new($watchdir, 0, "FILE_NAME");
while (1) {
  if ($notify->wait(1_000)) {  # 1-second wait cycle
    notify->reset;
    @foundfiles = File::get_by_ext($watchdir, "csv");  # search and return files in $watchdir with extension "csv"
    print "Something has happened! (del/ren/create)\n";
    foreach (@foundfiles) {
      move($watchdir.$_, $someotherdir.$_) or die "Fehler: $!"; 
    }
    @foundfiles = ();
  }
}

有没有办法自动查明文件是否已准备就绪,即最终 created/copied?

我在想

while (1) {
  move $file if (-w $file)  # writeable
  wait(1)
}

但这在 Windows 下似乎不起作用。我需要在 Windows 和 Perl 下解决这个问题。除此之外,我愿意接受建议。

是的!我解决了(感谢 Сухой27)!

在移动文件之前插入以下代码:

while (1) {
    last if writeable($path_in.$_);
    print "-";
    $| = 1;
    sleep(1);
}

...而 writeable 指的是这艘小潜艇:

sub writeable {
    return open(my $file, ">>", shift);
}

谢谢,祝你有愉快的一天! :-)