有沒有辦法監視powershell背景中目錄中的文件更改。使用WatcherChangeTypes啓動作業
我試圖按照下面的方法做。
Start-Job {
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = get-location
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $false
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName
while($true){
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed -bor [System.IO.WatcherChangeTypes]::Renamed -bOr [System.IO.WatcherChangeTypes]::Created, 1000);
if($result.TimedOut){
continue;
}
Add-Content D:\receiver.txt "file name is $($result.Name)"
}
}
這不起作用。我沒有收到有關receiver.txt文件的任何信息。儘管如果我不使用開始工作,腳本仍按預期工作。
非常感謝!這對我有效。 – 2014-09-28 17:08:22