-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christophe CREMON
committed
Jul 6, 2012
1 parent
f49c379
commit f831b31
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |