【问题标题】:Loop until file exists using windows batch command使用 Windows 批处理命令循环直到文件存在
【发布时间】:2015-03-10 11:56:43
【问题描述】:

如何将以下代码转换为 windows 批处理命令?

这是一个 perl 脚本,它在 while 循环中搜索文件,如果找到则退出。

use strict;
use warnings;
my $filename = 'something.txt'; 
while (1) {

if (-e $filename) {
print "File Exists!";
   exit;
   }

}

【问题讨论】:

    标签: file batch-file while-loop command line


    【解决方案1】:

    这是一个相当直接的翻译。代码应该是不言自明的:

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

    【讨论】:

    • IF EXIST 缺少%。 OP原始代码没有延迟,那你为什么?如果您确实使用 TIMEOUT 引入了延迟,那么您应该将 stdout 重定向到 nul 并使用 /NOBREAK 选项。
    • @dbenham - 已更新。我对延迟做了一个假设。批处理很容易修改,所以我想为什么不直接添加它。
    • @dbenham - 在上面问这个问题时,我已经尝试过使用子例程并且我得到“批量递归超出堆栈限制”。我猜 TIMEOUT /T 60 解决了这个问题。
    • 如果我是你,我不会删除该超时。减少一点 - 说是 1 秒,但不要删除它。如果你删除它,CMD 将进入一个硬循环并且绝对吃掉 CPU。
    • @Mihir:如果您更改内容,请将代码编辑到您的原始问题中作为补充数据来显示您的代码。
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2018-01-09
    • 2020-09-29
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多