How to monitor a folder and trigger a command-line action when a file is created or edited?
Solution 1
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
- Create a new text file
- Copy & paste the above code
- 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
- folder to monitor:
- Save and rename it to
StartMonitoring.ps1
- 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
Solution 2
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!)
Solution 3
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.
Solution 4
If the action is just to copy changed files, you can use robocopy /MON:1
I don't know if robocopy uses FileSystemWatcher or works by polling for changes.
Solution 5
Also found watchman that seems to be pretty big, and a smaller watchexec I haven't tried.
Watchman feels nice and programmatic. A CLI utility for power users.
Related videos on Youtube
Comments
-
bigmattyh over 1 year
I need to set up some sort of a script on my Vista machine, so that whenever a file is added to a particular folder, it automatically triggers a background process that operates on the file. (The background process is just a command-line utility that takes the file name as an argument, along with some other predefined options.)
I'd like to do this using native Windows features, if possible, for performance and maintenance reasons. I've looked into using Task Scheduler, but after perusing the trigger system for a while, I haven't been able to make much sense of it, and I'm not even sure if it's capable of doing what I need.
I'd appreciate any suggestions. Thanks!
-
Keith over 13 yearsSounds like you need somethink like Linux's inotify, but for windows. Jnotify might help, but since it's Java might be too heavyweight.
-
Keith over 13 yearsI was wondering about this too... found the [MSDN page](msdn.microsoft.com/en-us/library/aa365261(VS.85).aspx).
-
Sun about 8 yearsLink above has ) after aspx: msdn.microsoft.com/en-us/library/aa365261(VS.85).aspx
-
-
barlop over 13 yearsnice idea.. btw, C:\>ping 1.1.1.1 -n 10 -w 6000 for some reason took 1min 10 seconds on my computer. but -n 1 -w 60000 took 1min exactly.
-
Thu Rein over 13 years@barlop - the ten second discrepancy is due to the
-n 10
vs-n 1
. -
barlop over 13 years@Randolph Potter Isn't 10 lots of 6 seconds, 60 seconds? and -n 10 that you use, should mean 10 times.
-
Thu Rein over 13 yearsYou're probably right. I take it back and blame my blond hair.
-
Snekse almost 12 yearsI ended up using this loop for a monitoring batch
IF NOT EXIST C:\NO_SUCH_FILE_EVER.foo
. Hackish, but it works. Thanks for the idea. -
Paul Matthews almost 9 yearsWas thinking about using this software. Would you trust it to monitor a network share with important files from many users?
-
Jan Stanstrup over 7 yearsWorks nicely but what is the function of the stop script? I get on errors: "Unregister-Event : Cannot bind argument to parameter 'SourceIdentifier' because it is null." pastebin.com/ugLB3a69
-
nixda over 7 years@JanStanstrup I probably confused more people with the second script. I'll delete it. It's enough to simply close your
StartWatching.ps1
window to stop the monitoring. The second script does only work if you include it in your first script to save the variables$created
,$changed
,$deleted
or$renamed
-
geisterfurz007 over 5 yearsNow that 1.1.1.1 is an actually used IP, you will have to use another IP address or timeout for a not so exact timeout.
-
PeterCo almost 5 yearsThe free version of Watch4Folder has only one "sample" entry which can be configured.
-
mayank1513 over 4 yearsThanks for the answer. Can anyone suggest if we could use cmd prompt as I want to upload the files to github when changed
-
Roland about 4 yearsError: Invalid Parameter: /mon
-
weberjn about 4 years
-
Roland about 4 yearsok, the option is not /MON, but /MON:1. I tried, and the default is also /MOT:1, for polling every 1 minute. /MOT:0 also polls every 1 minute. This clears up the issue of FileSystemWatcher: no, it does polling.
-
Scott R about 4 yearsquite a few similar file and dir monitors at portablefreeware.com/index.php?sc=65
-
Sun about 4 yearsWhat's your point though? Did you see if any of the programs do what the OP wants? Otherwise, what's the point of linking to a bunch of folder watch programs... Dive deeper and provide an answer.
-
Stack Underflow about 4 yearsCombining this answer with A PowerShell Windows service, you won't have to leave a PowerShell window open to watch a directory.