1
我想監視在文件夾中創建的新文件。 當發生這種情況時,我想啓動一個批處理文件(在下面的示例中,我只是在日誌文件中寫入一行)。 我不知道爲什麼這不起作用。不同文件的差異操作
我的代碼是:
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "D:\"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
if ($file.Name -like "Apertura") {
Add-Content "C:\Users\F701845\Desktop\Apertura.txt" -Value $logline
} else {
Add-Content "C:\Users\F701845\Desktop\TestNO.txt" -Value $logline
}
}
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}
請說明它是如何失敗的 - 有一個在失敗的代碼沒有明顯的理由,因此任何相關的錯誤信息將是有益的。 –
我的猜測是,'$ file.Name-like「Apertura」'應該是'$ file.Name -like「* Apertura *」'''''''''不帶'*'作爲通配符與'-eq ' – BenH