Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If not exists then exit + cmd

Tags:

cmd

dos

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.

like image 743
Sebastian Avatar asked Jun 22 '10 08:06

Sebastian


3 Answers

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

like image 90
tanascius Avatar answered Nov 13 '22 19:11

tanascius


If you need to wait some seconds use standard CHOICE command. This sample code check if file exist each two seconds. The loop ends if file exists:

@ECHO OFF    
:CHECKANDWAITLABEL
IF EXIST myfile.txt GOTO ENDLABEL
choice /C YN /N /T 2 /D Y /M "waiting two seconds..."
GOTO CHECKANDWAITLABEL 

:ENDLABEL
like image 39
Livi Avatar answered Nov 13 '22 19:11

Livi


exit is a key word in DOS/Command Prompt - that's why goto exit doesn't work.

Using if not exist "file name" exit dumps you out of that batch file. That's fine if exiting the batch file is what you want.

If you want to execute some other instructions before you exit, change the label to something like :notfound then you can goto notfound and execute some other instructions before you exit.

(this is just a clarification to one of the examples)

like image 33
Bryan Avatar answered Nov 13 '22 19:11

Bryan