有没有办法监视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