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 have some batch scripts which wait for files. The wait loop is done with the typical IF EXISTS loop:

IF EXISTS file.zip GOTO waitloopend
sleep.exe 60
goto waitloop
: waitloopend

I am looking for a more efficient way of waiting for files. Something like a waitfile.exe command which will block and wait until the file appears. Internally, it should use the FileSystemWatcher class to be able to exit as soon as the file appears.

In Linux I have my own Perl script which internally uses Inotify.

Do you know if there exists a tool like this?

share|improve this question
You cna probably safely reduce the waiting time to 1 second for quicker response. Checking for file existance isn't that much of a burden on the system if you do it once every second instead of once every minute. –  Joey Jun 21 '11 at 12:11
The files are in a remote file server (samba). Some time ago i had to increase the sleeping time from 5 to 60 seconds because of fileserver overload. I have dozens of processes waiting for files to appear in the fileserver. Probably i should be using some kind of IPC instead of polling... –  Francisco R Jun 21 '11 at 13:17
Ah, ok, SMB is an argument, especially the ancient version. SMB 2 did a lot to reduce latency problems, but afaik Sambe isn't there yet, despite it being 4 years old by now –  Joey Jun 21 '11 at 18:47
Does this help: superuser.com/questions/226828/… ? - there's a .vbs file there that might solve your issue. –  Remco Apr 24 '14 at 9:26

2 Answers 2

A typical file system monitor detects file creations and if the file will be created while the tool is active you will receive the event, but if you will start the tool with an already existing file, there will be no subsequent creation events and the tool will wait forever.

It looks like you need to implement a custom tool, which will first check if the file already exists and if not start monitoring for the file.

share|improve this answer
Thanks for your answer, but i'm asking for an already made tool, so "implement a custom tool" is not a valid answer, sorry. –  Francisco R Jun 24 '11 at 9:48

Use PowerShell to access the FileSystemWatcher API.

#By BigTeddy 05 September 2011 

#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
#The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
#The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
#The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
#You need not subscribe to all three types of event.  All three are shown for example. 
# Version 1.1 

$folder = 'c:\scripts\test' # Enter the root path you want to monitor. 
$filter = '*.*'  # You can enter a wildcard filter here. 

# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

# Here, all three events are registerd.  You need only subscribe to events that you need: 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 

Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore red 
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 

Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore white 
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 

# To stop the monitoring, run the following commands: 
# Unregister-Event FileDeleted 
# Unregister-Event FileCreated 
# Unregister-Event FileChanged

Found here: https://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b

share|improve this answer
An acceptable answer should not rely on third party links which may break sometime in the future - can you correct? –  suspectus Jun 18 at 12:39

Your Answer


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.