How to monitor a folder and trigger a command-line action when a file is created or edited?

At work we use Powershell to monitor folders.
It can be used since Windows Vista (.NET and PowerShell is preinstalled) without any additional tools.

This script monitors a certain folder and writes a logfile. You can replace the action and do whatever you want e.g call an external tool

Example log file

11/23/2014 19:22:04, Created, D:\source\New Text Document.txt
11/23/2014 19:22:09, Changed, D:\source\New Text Document.txt
11/23/2014 19:22:09, Changed, D:\source\New Text Document.txt
11/23/2014 19:22:14, Deleted, D:\source\New Text Document.txt

StartMonitoring.ps1

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "D:\source"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\log.txt" -value $logline
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

How to use

  1. Create a new text file
  2. Copy & paste the above code
  3. Change the following settings to your own needs:
    • folder to monitor: $watcher.Path = "D:\source"
    • file filter to include only certain file types: $watcher.Filter = "*.*"
    • include subdirectories yes/no: $watcher.IncludeSubdirectories = $true
  4. Save and rename it to StartMonitoring.ps1
  5. Start monitoring by Right click » Execute with PowerShell

To stop monitoring, it's enough to close your PowerShell window

Further reading

  • Documentation for PowerShell's FileSystemWatcher
  • Documentation for PowerShell's Register-Event
  • Inspirations for script

You seem to be on the right lines - you could use the task scheduler to run a .bat or .cmd file on a regular basis and that file could start with a line to check for the existence of the required file - in fact, I'd check for the non existence of the file; for example:

@ECHO OFF
REM Example file
IF NOT EXIST C:\SOMEWHERE\SUBFOLDER\THISFILE.THS EXIT 1
REM All this gets done if the file exists...
:
:
EXIT 0

You could also modify this code and have it run in a loop with a, say, 1 minute delay in the loop and then put a reference to the batch file in the Windows startup folder:

@ECHO OFF
REM Example file
:LOOP    
IF NOT EXIST C:\SOMEWHERE\SUBFOLDER\THISFILE.THS GOTO SKIP01
REM All this gets done if the file exists...
:
:
:SKIP01
REM Crafty 1 minute delay...
PING 1.1.1.1 -n 10 -w 6000 >NUL
GOTO LOOP

There are other ways of achieving a delay according to the version of Windows running and what additional resource kits have been installed, but the PING command pretty much works under all circumstances. In the PING command above, 10 phantom PINGS are executed with a delay of 6000ms (ie: 6 seconds) between them, you can play with these values to achieve the delay you need between batch file loops.


Thanks all, for the suggestions.

I ended up writing a VBScript that was roughly based on Linker3000's idea of polling the folder, and using the Task Scheduler to have it run on startup. I ended up getting the basic syntax from this resource and made the requisite tweaks.

I'd still like to optimize it at some point, having the guts of the script run on an event-driven system, but I've run out of time to work on it, and, well, this is good enough.

Here's the script, in case anyone's interested (with the irrelevant conversion segment redacted for clarity):

' FOLDER TO MONITOR
strFolder = "J:\monitored-folder"

' FREQUENCY TO CHECK IT, IN SECONDS
nFrequency = 10

strComputer = "."
strQueryFolder = Replace(strFolder, "\", "\\\\")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &     strComputer & "\root\cimv2") 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery ("SELECT * FROM __InstanceCreationEvent WITHIN " & nFrequency & " WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent='Win32_Directory.Name=""" & strQueryFolder & """'") 

Do 
    Set objLatestEvent = colMonitoredEvents.NextEvent
    strNewFile = objLatestEvent.TargetInstance.PartComponent
    arrNewFile = Split(strNewFile, "=")
    strFilePath = arrNewFile(1)
    strFilePath = Replace(strFilePath, "\\", "\")
    strFilePath = Replace(strFilePath, Chr(34), "")
    strFileName = Replace(strFilePath, strFolder, "")
    strTempFilePath = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\TEMP.M4A"

    ' DO THE OPERATION STUFF
    ' ...
Loop

(Also, I don't want to leave this question officially unanswered -- and I hate to accept my own answer to the question -- but I did upvote Linker3000's answer as a thanks!)