FileSystemWatcher - Subdirectories

This topic contains 1 reply, has 1 voice, and was last updated by  James Krusic 9 months, 2 weeks ago.

  • Author
    Posts
  • #61215

    James Krusic
    Participant

    Hello,

    I am trying to figure out why FileSystemWatcher will not monitor subdirectories for the parent. I have set IncludeSubdirectories = $true, but it is still only searching the parent.

    Folder Structure (D = Directory, F = File)
    D –> Resource
    D –>> Archive (Resource\Archive)
    D –>> MoveIT (Resource\MoveIT)
    D –>> Source (Resource\Source)
    D –>>> Date (Resouce\Source\Date)
    F –>>>> *.csv

    Case: If I place the *.csv into Resource\Source directory FileSystemWatcher identifies and takes action. If I place the *.csv in Resource\Source\\*.csv filesystemwatcher does not walk the tree and trigger.

    Please keep in mind this is rough code and needs cleaned up, just trying to identify the issue.

    Function Wait-Change
    {
        
        Param
        (
            [Parameter(Mandatory=$true)]
            [String]$Path,
    
            [Parameter(Mandatory=$true)]
            [String]$Filter
        )
    
    
        $FileWatcher = New-Object System.IO.FileSystemWatcher
        $FileWatcher.Path = $Path
        $FileWatcher.Filter = $Filter
        $FileWatcher.IncludeSubdirectories = $true
        $FileWatcher.EnableRaisingEvents = $true
        $FileWatcher.NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    
        $OnChange = Register-ObjectEvent $FileWatcher Created -SourceIdentifier FileCreated -Action {
                $name = $Event.SourceEventArgs.Name
                $changeType = $Event.SourceEventArgs.ChangeType 
                $timeStamp = $Event.TimeGenerated
                $Destination = "D:\Resouce\Archive\$name"
                $folder = $Event.SourceEventArgs.FullPath
    
                Move-Item -Path $folder -Destination $Destination -Force
    
                Try
                {
                    Write-Verbose "Conversion #1"
                    #Replace | to ","
                    UpdateConfig -path $Destination -source "|" -sourcechanged '","' -file_extension $Filter -RemoteDestination 'D:\Resource\MoveIT' -Verbose 
                }
                Catch
                {
                    "Failed Conversion #1"
                }
    
            }
    
        While($Global:FileChanged -eq $False)
        {
            Start-Sleep -Milliseconds 100
        }
    
    }
    
    Wait-Change -Path "D:\Resource\Source\" -Filter "*.csv" -Verbose
    
  • #61221

    James Krusic
    Participant

    After further testing, it appears that when a file is created in the sub-directory such as D:\Resource\Source\Date\*.csv FileSystemWatcher begins to fail to monitor the parent directory, resulting needing to unregister then re-register the events.

You must be logged in to reply to this topic.