Categories

Watching the file arrival

I picked up a question in the ITKE forums about a script to watch the file system

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016

function eventhandler {
param ($e)
 Write-Host "File: $($e.SourceEventArgs.FullPath) has arrived"
}


$folder = "C:\test"
$filter = "*"
$fsw = New-Object -TypeName System.IO.FileSystemWatcher `
-ArgumentList $folder, 
$filter
$fsw
.IncludeSubDirectories = 
$true

$action
 = {eventhandler $($event)}

Register-ObjectEvent -InputObject $fsw -EventName "Created" `
 -SourceIdentifier "File System Creation" -Action $action

 

The event handler function accepts the event as a parameter and writes out that a file has been received. You can put anything in here eg a mail message could be sent.

The folder and filter (all files) are set and we define the FileSystemWatcher object which we set to include subdirectories

The action is defined and the event is registered.

dot source the script when you run it

Leave a Reply