Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question
    
Sounds like you need somethink like Linux's inotify, but for windows. Jnotify might help, but since it's Java might be too heavyweight. –  Keith Dec 29 '10 at 10:33
    
I was wondering about this too... found the [MSDN page](msdn.microsoft.com/en-us/library/aa365261(VS.85).aspx). –  Keith Dec 29 '10 at 10:38

3 Answers 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.

share|improve this answer
    
nice 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. –  barlop Dec 29 '10 at 12:14
    
@barlop - the ten second discrepancy is due to the -n 10 vs -n 1. –  user3463 Dec 30 '10 at 2:24
    
@Randolph Potter Isn't 10 lots of 6 seconds, 60 seconds? and -n 10 that you use, should mean 10 times. –  barlop Dec 30 '10 at 9:13
    
You're probably right. I take it back and blame my blond hair. –  user3463 Dec 30 '10 at 20:52
    
I 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. –  Snekse Jul 1 '12 at 0:29
up vote 1 down vote accepted

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!)

share|improve this answer
1  
nothing wrong with accepting your own answer –  barlop Jan 7 '11 at 10:07

Or you could use Watch 4 Folder. Apparently it's Freeware, portable, and compatible with Windows 7. I haven't tried it, but found it through a web search and thought I'd pass it on.

I like the VBS script too, also featured on the site.

share|improve this answer
    
Unfortunately only the paid version is portable –  nixda Jul 12 '13 at 10:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.