2015-02-24 71 views
-1

我想在Perl編譯Windows中的看門狗下一個熱文件夾(我可以稱之爲文件夾 - 手錶或者,嗯,可能要好得多:一個熱狗)。 到目前爲止,我成功地做到了這一點,Win32::ChangeNotify(請參閱下面的示例)。找出文件是否可寫/移動

但是,正如你可能已經猜到閱讀源代碼,我遇到了一個問題,當行駛過程中要當複製/在$watchdir創建文件的過程還沒有完成(沒有這樣的文件或目錄)來完成。

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 =(); 
    } 
} 

有沒有辦法自動找出文件是否準備好去,即最終創建/複製?

我在思考着什麼樣

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

但似乎無法在Windows下工作。我需要在Windows和Perl下解決這個問題。除此之外,我願意接受建議。

+0

嗯,也許通過檢查是否可以用文件句柄打開文件...!? – 2015-02-24 15:52:31

+1

'sub writeable {打開我的$ fh,>> >>,shift}'':)' – 2015-02-24 15:54:31

+0

我不知道你爲什麼認爲'-w'應該告訴你沒有人寫入文件。有可能是操作系統特定的方式來檢查一個文件是否正在使用,但我不知道它們。 – ikegami 2015-02-24 16:16:57

回答

1

是的!我解決了它(感謝Сухой27)!

向右移動文件前插入下面的代碼:

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

...而writeable指的是這個小亞海洋:

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

感謝,並有一個尼夫一天! :-)