CODE‎ > ‎Shellscript‎ > ‎

PowerShell監控目錄內檔案

Quick snippet of PowerShell 2.0 code to register for file system events using the .Net framework:

#REQUIRES -version 2.0
##file system watcher
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "c:\"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$watcher
Register-ObjectEvent $watcher "Changed" -Action { Write-Host "Changed file $($eventArgs.Name)" }
Register-ObjectEvent $watcher "Created" -Action { Write-Host "Created file $($eventArgs.Name)" }
Register-ObjectEvent $watcher "Deleted" -Action { Write-Host "Deleted file $($eventArgs.Name)" }
Register-ObjectEvent $watcher "Renamed" -Action { Write-Host "Renamed file $($eventArgs.Name)" }

Comments