From f831b3106ef36c3ad3792f9ee65c446669a17148 Mon Sep 17 00:00:00 2001 From: Christophe CREMON Date: Fri, 6 Jul 2012 11:44:08 +0200 Subject: [PATCH] PowerFileWatcher --- PowerFileWatcher/PowerFileWatcher.psm1 | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 PowerFileWatcher/PowerFileWatcher.psm1 diff --git a/PowerFileWatcher/PowerFileWatcher.psm1 b/PowerFileWatcher/PowerFileWatcher.psm1 new file mode 100644 index 0000000..a87a907 --- /dev/null +++ b/PowerFileWatcher/PowerFileWatcher.psm1 @@ -0,0 +1,52 @@ +# File Watcher PowerShell Module +# http://powershell.codeplex.com + +Function Start-FileWatcher +{ + param ( [string] $FileName = $(throw "Please specify a File Name"), + [int] $TimeOut = $(throw "Please specify a TimeOut in Seconds"), + [string] $ChangeType = $(throw "Please specify a Change Type [ Created | Deleted | Changed | Renamed ]"), + [switch] $IncludeSubDirectories ) + + $Loop = $true + $TimeOut = $TimeOut * 1000 + $ChangeTypes = @( "Created", "Deleted", "Changed", "Renamed" ) + + if ( $ChangeTypes -notcontains $ChangeType ) + { + throw "Please specify a Change Type [ Created | Deleted | Changed | Renamed ]" + } + + while ( $Loop -eq $true ) + { + $FileWatcher = New-Object System.IO.FileSystemWatcher + $FileWatcher.Path = Split-Path $FileName + $FileWatcher.Filter = Split-Path -Leaf $FileName + $FileWatcher.EnableRaisingEvents = $true + if ( $IncludeSubDirectories ) + { + $FileWatcher.IncludeSubdirectories = $true + } + $FileWatcherResult = New-Object System.IO.WaitForChangedResult + $FileWatcherResult = $FileWatcher.WaitForChanged($ChangeType,$TimeOut) + if ( $FileWatcherResult.ChangeType -eq $ChangeType ) + { + $RootName = $FileWatcher.Path + $TreeName = $FileWatcherResult.Name + $FullFileName = "$RootName\$TreeName" + if ( $RootName -match "\\$" ) + { + $FullFileName = "$RootName$TreeName" + } + return $FullFileName + $Loop = $false + } + if ( $FileWatcherResult.TimedOut -eq $true ) + { + return $false + $Loop = $false + } + } +} + +Export-ModuleMember -Function Start-FileWatcher \ No newline at end of file