Skip to content

Instantly share code, notes, and snippets.

@dlidstrom
Created Jun 15, 2016
Embed
What would you like to do?
watch a file changes in the current directory
# 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