System.IO.FileSystemWatcher 只发射了一次
System.IO.FileSystemWatcher fired only one time
要求如下
- 通过 FTP 客户端,用户将在 FTP 服务器上上传文件
- 复制文件后,我们需要对其进行处理并调用批处理文件
- 完成后,将等待另一个文件
下面的脚本运行对于一个文件来说没问题,但是对于下一个文件,没有动作事件不会被触发。
### 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 任务计划程序以连续 运行。
我不确定我在这里遗漏了什么?
回到更小的东西,这在没有 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
另外,睡眠也不是很需要。如果您不设置它,当然会立即移动。
要求如下
- 通过 FTP 客户端,用户将在 FTP 服务器上上传文件
- 复制文件后,我们需要对其进行处理并调用批处理文件
- 完成后,将等待另一个文件
下面的脚本运行对于一个文件来说没问题,但是对于下一个文件,没有动作事件不会被触发。
### 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 任务计划程序以连续 运行。
我不确定我在这里遗漏了什么?
回到更小的东西,这在没有 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
另外,睡眠也不是很需要。如果您不设置它,当然会立即移动。