Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a batch file which is triggered whenever a new file is added to a particular folder.

I can schedule a task using schtasks. But I am looking for a solution based upon interrupt kind of thing rather than polling. Is there any other way to do this?

share|improve this question
    
What OS are you trying to do this on? – slm Apr 7 '13 at 10:56
    
I am working on windows – patro Apr 7 '13 at 13:36
up vote 3 down vote accepted

There is a way to do what you want, it's called iNotify on Linux/Unix, where the OS can run a script based on activity in a directory that it is "watching". Windows and Mac have similar functionality, it's called by different names.

This question on stackoverflow covers all the options for the various OSes.

JNotify is a nice option which is written in Java and can run on all OSes. It requires writing some Java code, not sure if you're looking for that sort of option.

share|improve this answer
    
this was helpful...thanks! – patro Apr 7 '13 at 13:36

Batch can't polling, but you can try this VBScript:

Option Explicit

Const Path = "%userprofile%\Documents\Folder"
Const Interval = 1
Const Delay = 60

Dim oWSH, oFSO, oWMI, oEvent, oTarget, colEvents
Dim sPath, sDrive, sFolder, sNewPath

Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
sPath = oWSH.ExpandEnvironmentStrings(Path)
sPath = oFSO.GetAbsolutePathName(sPath)
If Not oFSO.FolderExists(sPath) Then oFSO.CreateFolder(sPath) 
sDrive = oFSO.GetDriveName(sPath)
sFolder = Replace(Mid(sPath, 3) & "\", "\", "\\", 1, -1, vbTextCompare)

Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colEvents = oWMI.ExecNotificationQuery( _
  "SELECT * FROM __InstanceCreationEvent" _
  & " WITHIN " & CStr(Interval) _
  & " WHERE Targetinstance ISA 'CIM_DataFile'" _
  & " AND TargetInstance.Drive='" & sDrive & "'"_
  & " AND TargetInstance.Path='" & sFolder & "'") 

Do 
  Set oEvent = colEvents.NextEvent()
  sNewPath = Year(Now) & "_" & Right("0" & Month(Now), 2) & "_" & Right("0" & Day(Now), 2)
  sNewPath = oFSO.BuildPath(sPath, sNewPath)
  If Not oFSO.FolderExists(sNewPath) Then oFSO.CreateFolder(sNewPath)
  Set oTarget = oEvent.TargetInstance
  WScript.Sleep Delay * 1000
  On Error Resume Next
  oFSO.MoveFile oTarget.Name, oFSO.BuildPath(sNewPath, oFSO.GetFileName(oTarget.Name))
  On Error Goto 0
Loop
share|improve this answer

We use the tool at http://www.myassays.com/folder-poll to do just this. It's a Windows application that includes a user-friendly manager application to allow easy configuration. Also, there is an XML configuration option. The actual folder polling runs as a Windows service (so starts automatically on each restart). When a new file is detected in a polled folder an application can be launched automatically (you can specify your own custom command line arguments). It can do other things like copy/move files too. Also, activity can be logged to a log file and there are other advanced operations. Note, this is a commercial tool (i.e. not free).

share|improve this answer
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – suspectus Aug 27 '15 at 9:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .