4

我正在使用 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
4

1 回答 1

4

改变这个:

$(watcher.Path)backup-$datestamp"

$($sender.Path)backup-$datestamp"

请注意,里面的变量$()仍然需要一个$. 并且$sender自动变量将始终包含生成事件的对象。

于 2013-11-25T17:21:28.997 回答