开发者

If not exists then exit + cmd

开发者 https://www.devze.com 2023-01-04 12:55 出处:网络
i try to make a loop in a .cmd file. If test.txt is not exists then i will kill the cmd process开发者_Go百科.

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

If test.txt is not exists then i will kill the cmd process开发者_Go百科.

@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.


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


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


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)


Using the following:

if not exist "file name" goto exit

Results in:

The system cannot find the batch label specified - exit

However using the same command without "goto" works, as follows:

if not exist "file name" exit
0

精彩评论

暂无评论...
验证码 换一张
取 消