-1

I need a way to track updates/modifications to a file in real time and run a batch script when they take place, I have tried this to no avail, which may be due to it being open in the program that is logging output to the file, but

Get-content filename -Tail 0 -Wait

in powershell will show me any updates that happen to the file, is there any way for me to make it launch a script when a change is detected or any other way to do so. I need it to be in either batch or powershell script.

user1166618
  • 23
  • 1
  • 7
  • In *nix I'd use the stat command to look at the last modified time via stat - according to this you can do that in batch/powershell: https://stackoverflow.com/questions/1520643/windows-batch-file-check-if-file-has-been-modified – MaQleod Apr 23 '20 at 04:40

1 Answers1

0

Use a file system watcher and take action on the event.

Powershell FileSystemWatcher

https://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b

This script uses the .net FileSystemWatcher class to subscribe to 'create', 'change' and 'delete' events on files or folders in the NTFS filesystem.It can be used to monitor a folder or folders, and can be modified to perform any action upon the triggering of these events.

Download: FileSystemWatcher.ps1

https://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b/file/42206/1/FileSystemWatcher.ps1

postanote
  • 4,589
  • 2
  • 7
  • 7
  • I've tried that already – user1166618 Apr 23 '20 at 04:50
  • @user1166618 - so ... what did not work as you expected AND exactly what code did you use? "I've tried that already" is nearly useless ... you need to add what you did & what failed to work as expected. – Lee_Dailey Apr 23 '20 at 06:00