How to monitor a folder for changes, and execute a command if it does, on Windows?

31

16

There are similar questions for Linux and Mac, but I'm after a Windows solution here.

The problem is as follows: I want to write several (js) script files in a folder, and have a program monitor that folder for file changes and new files being added, and run a command whenever that happens (to compile them all into one single file).

The solution has to:

  1. Monitor both file changes and new files being added, in a folder.
  2. Run a command only if there is any change.

It would be best if it either is a built-in solution (like a JScript or VBscript snippet), or something that does not require installation.

Camilo Martin

Posted 2012-08-31T14:36:11.017

Reputation: 2 308

see this blog for some apps for windows.

– Vijay – 2014-09-04T09:29:38.203

Answers

3

There's a program called Belvedere that might do the trick.

http://ca.lifehacker.com/341950/belvedere-automates-your-self+cleaning-pc

It's a stand alone Windows app that runs in the background.

I've set it up to monitor my downloads folder for files with certain extensions with last modified dates of a day old. The files it finds, it deletes, however you could have it run an action instead.

dangowans

Posted 2012-08-31T14:36:11.017

Reputation: 1 774

Wow - that one looks super nice. I'll choose this as an answer (even if I already got what I needed from another one) since it probably is much more useful and versatile than the one I'm using, for other people (and maybe me in the future!). +1 and accepted! – Camilo Martin – 2012-08-31T15:07:27.177

2

Belvedere made me lose hours to eventually figure out it's full of unsolved bugs, especially with custom commands. Try FolderMonitor instead (and use "{0}{1}" in the parameter field, including the quotes). Only issue: you can't define a separate command for each folder. Just one, globally.

– fregante – 2014-05-14T03:04:19.340

@bfred.it Belvedere does have its issues, but it worked for me. FolderMonitor looks like a good solution too. I think it's the one the user asking the question ended up with.

– dangowans – 2014-05-14T18:12:48.997

13

I've had good success with an old program called Log Monitor. It's ancient, and long ago abandoned. But it serves the purpose pretty well.

http://www.freeware-guide.com/download/index2.html

Bob

Posted 2012-08-31T14:36:11.017

Reputation: 301

this awsome "thing" is working perfectly on windows 10 ! – timmz – 2016-10-05T21:11:41.623

1

Ah, thanks. I'm currently trying out another utility, but +1!

– Camilo Martin – 2012-08-31T14:59:28.240

7

I have created a simple utility for this purpose: https://github.com/benblamey/when_changed

usage: when_changed (file path) (command) (optional-parameters)

e.g. when_changed C:\somedir\foo.txt myapp.exe bar wibble 123

Ben

Posted 2012-08-31T14:36:11.017

Reputation: 181

I like how that's an elegant API. Does it support folders recursively? – Camilo Martin – 2014-07-14T10:44:33.277

Yes: when_changed C:\**.txt foo.exe

I wanted a no-dep version of https://github.com/joh/when-changed for windows.

– Ben – 2014-07-14T14:06:09.300

6

With .NET watchfolders this is really easy to code. I'm sure someone have done such a program.

Just found this gem for you http://www.guidingtech.com/9861/automate-folder-actions-windows-watch-4-folder/

bokan

Posted 2012-08-31T14:36:11.017

Reputation: 299

I'm aware of how easy it is to code this in .NET - I just didn't want to. But I'm trying this one which is free, may just work too! +1 for the recommendation, that one seems nice too.

– Camilo Martin – 2012-08-31T15:01:13.530

4

I'm currently trying this app, which requires .NET, but looks like it does the job.

Camilo Martin

Posted 2012-08-31T14:36:11.017

Reputation: 2 308

2

If you want to do a built-in solution using JScript or VBScript, then what you want to look for is file system monitoring using WMI event subscriptions. Basically you write the code to monitor a folder and preform actions when a change is detected such as:

  • __InstanceCreationEvent
  • __InstanceDeletionEvent
  • __InstanceModificationEvent

...and your script runs on an interval watching for these events to occur. There are many resources, but here are some for VBScript and Powershell:

mrTomahawk

Posted 2012-08-31T14:36:11.017

Reputation: 326

2

This is such a common requirement that I'm surprised there's no convenient utility built into the OS itself.

Anyway as mentioned earlier I've used LogMonitor successfully in the past. However I found this blog post informative and am using Watch 4 Folder now.

Hopefully this helps you out.

Vijay

Posted 2012-08-31T14:36:11.017

Reputation: 842

+1, but I wouldn't say it's a "common requirement" for anyone except us developers... And for that matter, there are system calls to monitor the filesystem, so that counts as "OS integration" I guess. – Camilo Martin – 2014-09-04T10:44:15.510

1Well I was seeing it from businesses & network share's perspective. You always want to know who to blame when a files goes missing :P. I am in the middle of diagnosing such a situation right now :/ – Vijay – 2014-09-04T10:48:18.597

2

Yet another tool: https://github.com/yankee42/java-file-change-watcher

Advantages: Platform independent (Java), small (10KB).

Disclaimer: I am the author

yankee

Posted 2012-08-31T14:36:11.017

Reputation: 595

1

NirSoft has software for monitor files changes on Windows
FolderChangesView

FolderChangesView is a simple tool that monitors the folder or disk drive that you choose and lists every filename that is being modified, created, or deleted while the folder is being monitored. You can use FolderChangesView with any local disk drive or with a remote network share, as long as you have read permission to the selected folder.

enter image description here

you can download it for free from here
More Info

AminM

Posted 2012-08-31T14:36:11.017

Reputation: 502

Seems more of an informational/debugging tool than an automation one, but still interesting. – Camilo Martin – 2015-05-23T03:29:18.987

0

You can use http://jnotify.sourceforge.net/ which subscribes to os-specific filesystem monitoring events. The code is years old but still works (at least on the Windows Server 2013r2 I was testing it yesterday).

There is also github resurrection of jnotify project on https://github.com/redbooth/jnotify which does not offer standalone demo app but (according to commit logs) patches some bugs.

In java7 there should be some kind of inotify-like filesystem watch too, but I was not testing it yet.

andrej

Posted 2012-08-31T14:36:11.017

Reputation: 422