If not exists then exit + cmd

Sebastian picture Sebastian · Jun 22, 2010 · Viewed 30.4k times · Source

i try to make a loop in a .cmd file.

If test.txt is not exists then i will kill the cmd process.

@echo off
if not exists test.txt goto exit

But this code doesn't work and i don't know how to make a loop every 2 seconds.

Thanks for help.

Answer

tanascius picture tanascius · Jun 22, 2010

The command is called exist, not exists:

if not exist test.txt goto :exit
echo file exists
:exit

About your loop:
I am not 100% sure, but I think there is no sleep or wait command in Windows. You can google for sleep to find some freeware. Another possibility is to use a ping:

ping localhost -n 3 >NUL

EDIT:
The Windows Server 2003 Resource Kit Tools contains a sleep.
See here for more information, too