File 使用windows批处理命令循环直到文件存在
如何将以下代码转换为windows批处理命令 下面是一个perl脚本,它正在while循环中搜索一个文件,如果发现它存在的话File 使用windows批处理命令循环直到文件存在,file,batch-file,while-loop,command,line,File,Batch File,While Loop,Command,Line,如何将以下代码转换为windows批处理命令 下面是一个perl脚本,它正在while循环中搜索一个文件,如果发现它存在的话 use strict; use warnings; my $filename = 'something.txt'; while (1) { if (-e $filename) { print "File Exists!"; exit; } } 这是一个相当直接的翻译。代码应该是非常不言自明的: @ECHO OFF SET LookForFile="C:
use strict;
use warnings;
my $filename = 'something.txt';
while (1) {
if (-e $filename) {
print "File Exists!";
exit;
}
}
这是一个相当直接的翻译。代码应该是非常不言自明的:
@ECHO OFF
SET LookForFile="C:\Path\To\File.txt"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 60 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 60 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
如果存在,则缺少一个
%
。OP原始代码没有延迟,那你为什么这么做?如果您确实使用TIMEOUT引入了延迟,那么您应该将stdout重定向到nul并使用/NOBREAK选项。@dbenham-Updated。我对延误作了一个假设。批处理很容易修改,所以我想为什么不直接添加它。@dbenham-上面提到的这个问题我已经尝试过使用子例程,我得到了“批处理递归exeeds堆栈限制”。我想TIMEOUT/T 60解决了这个问题。如果我是你,我不会删除那个超时。减少一点,比如说1秒,但不要移除它。如果您删除它,CMD
将进入一个硬循环并完全消耗CPU。@Mihir:如果您更改了内容,请将代码作为补充数据编辑到原始问题中,以显示代码。