我试图制作一个非常简单的bat文件。批处理文件 - 等待循环中的文件
在for循环中,我调用另一个应用程序为我创建一个文件。当生成该文件时,我想再次执行此操作,直到循环结束。在我再次运行循环之前,我需要等待文件的创建。
我需要使用START
和CALL
,因为在文件创建后,我会杀死进程。它不包含在下面的代码中,但它是必需的。
@echo off
for /L %%i in (1,1,5) do (
SET filename=fooFile_%%i.txt
START /B CMD /C CALL createFile.bat fooFile_%%i.txt
:waitfile
ECHO waitforfile: %filename%
TIMEOUT /T 10 >nul
IF EXIST %filename% GOTO FoundIt
goto waitfile
:FoundIt
ECHO Found file %filename%
)
ECHO All done!
而我的createFile.bat
。这实际上是另一种应用。但是,下面的代码就足以嘲笑它:
@echo off
set arg1=%1
TIMEOUT /T 10 >nul
copy NUL %arg1%
ECHO Created file
电流输出:
waitforfile:
waitforfile fooFile_1.txt
1 file(s) copied.
Created file
Found file fooFile_1.txt
All done!
正如你可以从输出看到我不能循环使用我的GOTO的工作。我已经看到了这些问题:
Batch file goto not working in for loop
(Windows batch) Goto within if block behaves very strangely
看来,因为它是一个坏主意,被goto的循环相结合。那么我该如何解决我的问题?
有在你的代码的两个问题:1,你错过了使用[延迟扩展(HTTP:// ss64。 com/nt/delayedexpansion.html),尽管在编写*和*时需要这样做,并在代码块中读取相同的变量; 2.'goto:Label'不能在块/循环中使用,因为这会中断块/循环上下文; – aschipfl
@aschipfl - 谢谢。我是新来的蝙蝠文件。我会尝试建议的解决方案。看起来他们都工作。在我接受它作为答案之前,我只需要确定哪种解决方案最适合我的具体问题。 – smoksnes