我正在尝试编写一个脚本,该脚本坐在那里并等待将文件添加到文件夹中。如果它检测到任何东西,我希望它然后将它们移动到另一个文件夹。
网上有不少文章的代码类似于下面显示的代码,但是如果您一次将多个文件放入文件夹中,这些似乎都不起作用。
$folder = "d:\Logs\temp"
$filter = "*.xml" # <-- set this according to your requirements
$destination = "d:\Logs\XML\"
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $false # <-- set this according to your requirements
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp"
Move-Item $path -Destination $destination -Force -Verbose # Force will overwrite files with same name
}
它们不仅不起作用,而且似乎完全被卡住了。有谁知道如何做到这一点?就我而言,一整批的xml文件会一次性存入文件夹,需要依次处理。
谢谢!