Batch file Oneliner等待创建文件
我需要一个单行程序来等待文件创建 有没有办法将其写入windows批处理命令?因为我用的是pushd,所以我不能在里面跳 比如:Batch file Oneliner等待创建文件,batch-file,Batch File,我需要一个单行程序来等待文件创建 有没有办法将其写入windows批处理命令?因为我用的是pushd,所以我不能在里面跳 比如: IF NOT EXIST \\blablabal\myfile.txt sleep(30) 我想你会想的。使用调用,如下所示: ( rem some code here call :waitForFile rem another code here ) rem yet another code here rem next `goto
IF NOT EXIST \\blablabal\myfile.txt sleep(30)
我想你会想的。使用
调用
,如下所示:
(
rem some code here
call :waitForFile
rem another code here
)
rem yet another code here
rem next `goto` skips `:waitForFile` subroutine; could be `goto :eof` as well
goto :nextcode
:waitForFile
IF EXIST \\blablabal\myfile.txt goto :eof
TIMEOUT /T 30 >NUL
goto :waitForFile
:nextcode
但是,如果您需要一个oneliner来等待文件创建,请以windows批处理脚本的形式编写:将下一个代码段另存为waitForFile.bat
@ECHO OFF
SETLOCAL EnableExtensions
:waitForFile
IF EXIST "%~1" ENDLOCAL & goto :eof
TIMEOUT /T 30 >NUL
goto :waitForFile
按如下方式使用:
(
rem some code here
call :waitForFile
rem another code here
)
rem yet another code here
rem next `goto` skips `:waitForFile` subroutine; could be `goto :eof` as well
goto :nextcode
:waitForFile
IF EXIST \\blablabal\myfile.txt goto :eof
TIMEOUT /T 30 >NUL
goto :waitForFile
:nextcode
- 从命令行窗口:
waitForFile“\\blablabal\myfile.txt”
- 从另一个批处理脚本:
callwaitforfile“\\blablabal\myfile.txt”
确保
waitForFile.bat
存在于当前目录或中的某个位置。下面的解决方案是一个一行程序,应根据请求在命令提示符下执行(批处理文件不是“一行程序”):
如果愿意,您可以在批处理文件中插入这一行,使百分号加倍。PowerShell中会有
while(-not(Test Path myfile.txt)){Sleep-Seconds 1}
-pushd如何与goto不兼容?我不明白为什么不能使用goto。如果我在powershell中使用批处理文件,也许您应该向我们展示您的完整批处理文件,那么是否也可以使用pushd连接到驱动器?您需要一个单行程序,因此我想您需要在命令提示符(cmd
)中执行它;这是真的吗?如果是这样,您应该相应地调整标签(添加?)goto
在cmd
中不起作用,所以我认为这不能直接在cmd
中完成…goto:mylabel
应该是goto:waitForFile
。