要求如下
下面的脚本对于一个文件运行正常,但是对于下一个文件,则不会触发任何动作事件。
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\folder\xmls"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
### Sleep for 30 seconds
Start-Sleep -s 30
$changeType = $Event.SourceEventArgs.ChangeType
$date = Get-Date
$logFile = "C:\folder\Log_" + $date.ToString("yyyyMMdd") + ".txt"
$logline = "$(Get-Date), $changeType, $path"
Add-Content -Path $logFile -Value $logline
$logline = "$(Get-Date), MLCP process started"
Add-Content -Path $logFile -Value $logline
Start-Process -FilePath 'C:\folder\import.bat' -ArgumentList @('"' + $path + '"') -Wait
$logline = "$(Get-Date), MLCP process completed"
Add-Content -Path $logFile -Value $logline
$destinationPath = "C:\folder\ProcessedXmls"
###Copy file to processed folder
Move-Item -Path $path -Destination $destinationPath -Force
$logline = "$(Get-Date), File moved to processed folder"
Add-Content -Path $logFile -Value $logline
$logline = "$(Get-Date), Call Upload script"
Add-Content -Path $logFile -Value $logline
$scriptToRun = "C:\folder\UploadStatustoBlob.ps1"
&$scriptToRun
Log($logFile, "Upload script completed from function")
$logline = "$(Get-Date), Upload script completed"
Add-Content -Path $logFile -Value $logline
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$created = Register-ObjectEvent $watcher Created -Action $action
while ($true) {sleep 1}
如果我再次重新启动PowerShell桌面应用程序,则它适用于第一个文件。
然后我需要将其添加到Windows Task Scheduler中以连续运行。
我不确定我在这里想念什么吗?
答案 0 :(得分:0)
回到更简单的方法,这无需While。
$Source = 'E:\Temp\folder\xmls'
$Filter = '*.*'
$destination = 'E:\Temp\Folder\ProcessedXmls'
$Watcher = New-Object IO.FileSystemWatcher $Source, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
Start-Sleep -Seconds 3
$Path = $Event.SourceEventArgs.FullPath
Move-Item $Path -Destination $destination -verbose
}
# Results
VERBOSE: Performing the operation "Move File" on target "Item: E:\Temp\folder\xmls\New Text Document.txt Destination: E:\Temp\Folder\ProcessedXmls\New Text Document.txt".
VERBOSE: Performing the operation "Move File" on target "Item: E:\Temp\folder\xmls\New Text Document - Copy.txt Destination: E:\Temp\Folder\ProcessedXmls\New TextDocument - Copy.txt".
VERBOSE: Performing the operation "Move File" on target "Item: E:\Temp\folder\xmls\New Bitmap Image.bmp Destination: E:\Temp\Folder\ProcessedXmls\New Bitmap Image.bmp".
添加我可以验证的最低限度的内容也是如此。
$Source = 'E:\Temp\folder\xmls'
$Filter = '*.*'
$destination = 'E:\Temp\Folder\ProcessedXmls'
$Watcher = New-Object IO.FileSystemWatcher $Source, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
Start-Sleep -Seconds 3
$Path = $Event.SourceEventArgs.FullPath
$date = Get-Date
$logFile = "E:\Temp\folder\Log_" + $date.ToString("yyyyMMdd") + ".txt"
$logline = "$(Get-Date), $changeType, $path"
Add-Content -Path $logFile -Value $logline
Move-Item $Path -Destination $destination -verbose
$logline = "$(Get-Date), File moved to processed folder"
Add-Content -Path $logFile -Value $logline
}
# Log file content --- Log_20190419.txt
04/19/2019 00:40:12, , E:\Temp\folder\xmls\New Text Document.txt
04/19/2019 00:40:12, File moved to processed folder
04/19/2019 00:40:28, , E:\Temp\folder\xmls\New Text Document - Copy.txt
04/19/2019 00:40:28, File moved to processed folder
04/19/2019 00:40:46, , E:\Temp\folder\xmls\New Bitmap Image.bmp
04/19/2019 00:40:46, File moved to processed folder
此外,睡眠并不是真正需要的。如果没有设置,移动当然是立即的。