我正在使用 aFileSystemWatcher
通知文件更改,然后创建该文件的副本:
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Orders\"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
$changed = Register-ObjectEvent $watcher "Changed" -Action {
write-host "Changed: $($eventArgs.FullPath)"
$datestamp = get-date -uformat "%Y%m%d%H%M%S"
write-host $datestamp
copy-item $eventArgs.FullPath "$(watcher.Path)backup-$datestamp"
}
因此,如果C:\Orders\orders.xml
被改变,那么C:\Orders\backup-20131125121004
应该被创建。但是,这不起作用,并且不会产生错误。通知确实有效,而不是副本:
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Documents and Settings\sladministrator\Desktop> .\WatchBizSyncOrders.ps1
PS C:\Documents and Settings\sladministrator\Desktop> Changed: C:\Orders\New Text Document.txt
20131125100821