[英]System.IO.FileSystemWatcher fired only one time
Requirement is as follows 要求如下
Below script is running fine for one file, but for next file, no action event is not fired. 下面的脚本对于一个文件运行正常,但是对于下一个文件,则不会触发任何动作事件。
### 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}
If I again restart PowerShell desktop app, it is working for 1st file. 如果我再次重新启动PowerShell桌面应用程序,则它适用于第一个文件。
I then need to add this to Windows Task Scheduler for continuous running. 然后,我需要将其添加到Windows Task Scheduler中以连续运行。
I am not sure what I am missing here? 我不确定在这里想念什么吗?
Going back to something more minimal, this works without the While. 回到更最小的东西,这无需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".
As does adding the bare minimum I could validate on my end. 和添加最低限度一样,我可以在最后进行验证。
$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
Also, the sleep is not really needed. 另外,睡眠并不是真正需要的。 Moves are immediate of course if you don't set that.
如果没有设置,移动当然是立即的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.