Skip to content

Instantly share code, notes, and snippets.

@kevduc
Created September 20, 2021 15:25
Show Gist options
  • Save kevduc/3eabb0cdf606d13ccb87b979700cae8b to your computer and use it in GitHub Desktop.
Save kevduc/3eabb0cdf606d13ccb87b979700cae8b to your computer and use it in GitHub Desktop.
Watch log files inside folder and copy them when changed
# From https://stackoverflow.com/questions/29066742/watch-file-for-changes-and-run-command-with-powershell
$folder = "C:\Users\LOCAL_~1\AppData\Local\Temp\3"
$filter = "*.log"
$destination = "C:\Users\Documents"
$Watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $false
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $Watcher -EventName Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp"
Write-Host $path
Copy-Item $path -Destination $destination -Force -Verbose
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment