Search code examples
powershellfile-watcher

System.IO.FileSystemWatcher fired only one time


Requirement is as follows

  • Thru FTP client, user will upload file on FTP server
  • Once file is copied, we need to process it and call batch file
  • Once done, will wait for another file

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.

I then need to add this to Windows Task Scheduler for continuous running.

I am not sure what I am missing here?


Solution

  • Going back to something more minimal, this works without the 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.