如果不存在则退出 +指令
我尝试在 .cmd 文件中创建一个循环。
如果 test.txt 不存在,那么我将终止 cmd 进程。
@echo off
if not exists test.txt goto exit
但这段代码不起作用,我不知道如何每 2 秒循环一次。
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用以下命令:
结果:
但是,使用不带“goto”的相同命令可以正常工作,如下所示:
Using the following:
Results in:
However using the same command without "goto" works, as follows:
该命令称为
exist
,而不是 exists:关于循环:
我不是 100% 确定,但我认为 Windows 中没有 sleep 或 wait 命令。您可以在 google 上搜索“sleep”来查找一些免费软件。另一种可能性是使用ping:
编辑:
Windows Server 2003 资源工具包工具 包含一个睡眠。
请参阅此处了解更多信息
The command is called
exist
, not exists: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:
EDIT:
The Windows Server 2003 Resource Kit Tools contains a sleep.
See here for more information, too
如果您需要等待几秒钟,请使用标准的CHOICE命令。此示例代码每两秒检查一次文件是否存在。如果文件存在则循环结束:
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:
exit 是 DOS/命令提示符中的关键字 - 这就是为什么 goto exit
不起作用。
使用如果不存在“文件名”退出会将您转出该批处理文件。
如果您想要退出批处理文件,那就没问题。
如果你想在退出前执行一些其他指令,请将标签更改为类似 :notfound 的内容,然后你可以转到 notfound
并在退出之前执行一些其他指令。
(这只是对其中一个示例的澄清)
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)