Batch real time wait for a file

I have some batch scripts which wait for files. The wait loop is done with the typical IF EXISTS loop:

:waitloop
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?


Your method is preferable and perfectly acceptable. FileSystemWatcher wastes resources, even more than your loop.

Even if you make your loop as tight as with one second delay, you'll still be totally unnoticeable in any process monitor that measures CPU or hard disk load.

BTW, you can use timeout command instead of sleep.exe.

Also, you have some typos in your code:

:waitloop
IF EXIST "scanning.done" GOTO waitloopend
timeout /t 1
goto waitloop
:waitloopend

Some info on 'wasting resources' can be found here: https://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes; the main point being that it could be unreliable. But I have to admit, my answer goes mainly from the years of practice and experience.