FileSystemWatcher - Subdirectories

Welcome Forums General PowerShell Q&A FileSystemWatcher - Subdirectories

This topic contains 1 reply, has 1 voice, and was last updated by

2 years, 3 months ago.

  • Author
  • #61215

    Points: 0
    Rank: Member


    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
        $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
                    Write-Verbose "Conversion #1"
                    #Replace | to ","
                    UpdateConfig -path $Destination -source "|" -sourcechanged '","' -file_extension $Filter -RemoteDestination 'D:\Resource\MoveIT' -Verbose 
                    "Failed Conversion #1"
        While($Global:FileChanged -eq $False)
            Start-Sleep -Milliseconds 100
    Wait-Change -Path "D:\Resource\Source\" -Filter "*.csv" -Verbose
  • #61221

    Points: 0
    Rank: Member

    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.

The topic ‘FileSystemWatcher - Subdirectories’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort