Created
June 15, 2016 10:12
-
-
Save dlidstrom/ed000dc49829e880db4a7d3208216655 to your computer and use it in GitHub Desktop.
watch a file changes in the current directory
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
# watch a file changes in the current directory, | |
# execute some command when a file is changed or renamed | |
param($BatFile) | |
if (($BatFile -eq $null) -or (-not (Test-Path -Path $Batfile))) | |
{ | |
"Specify bat file to run continuously." | |
exit | |
} | |
$watcher = New-Object System.IO.FileSystemWatcher | |
$watcher.Path = Get-Location | |
$watcher.IncludeSubdirectories = $false | |
$watcher.EnableRaisingEvents = $false | |
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName | |
& $BatFile | |
while ($true) { | |
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed -bor [System.IO.WatcherChangeTypes]::Renamed -bOr [System.IO.WatcherChangeTypes]::Created, 1000); | |
if( $result.TimedOut ) { | |
continue | |
} | |
# adjust if necessary | |
"Change in $($result.Name)" | |
if ($result.Name -Match ".png") { | |
"Skipping" | |
continue | |
} | |
& $BatFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment