【问题标题】:How to exit a SET /p in a batch file如何在批处理文件中退出 SET /p
【发布时间】:2015-03-24 11:28:28
【问题描述】:

如何在一定时间后退出 SET /p 命令?例如,如果用户输入内容的时间过长,我想退出 SET /p。

【问题讨论】:

    标签: batch-file cmd


    【解决方案1】:

    这是一个纯Batch的解决方案,避免了Batch-JScript混合脚本在使用Sendkeys且原始窗口不在焦点时出现的问题。

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Execute a SET /P command with time out
    rem Antonio Perez Ayala
    
    rem If this file is re-executed as pipe's right side, go to it
    if "%~1" equ "TimeoutMonitor" goto %1
    
    del InputLine.txt 2> NUL
    (
       set /P "line=You have 10 seconds to complete this input: " > CON
       > InputLine.txt call set /P "=%%line%%" < NUL
    ) 2> NUL | "%~F0" TimeoutMonitor 10
    set /P "line=" < InputLine.txt
    del InputLine.txt
    echo Line read: "!line!"
    goto :EOF
    
    
    :TimeoutMonitor
    
    rem Get the PID of pipe's left side
    tasklist /FI "IMAGENAME eq cmd.exe" /FO TABLE /NH > tasklist.txt
    for /F "tokens=2" %%a in (tasklist.txt) do (
       set "leftSidePipePID=!lastButOnePID!"
       set "lastButOnePID=%%a"
    )
    del tasklist.txt
    
    rem Wait for the input line, or until the number of seconds passed
    for /L %%i in (1,1,%2) do (
       ping -n 2 localhost > NUL
       if exist InputLine.txt exit /B
    )
    
    rem Timed out: kill the SET /P process and create a standard input line
    taskkill /PID %leftSidePipePID% /F > NUL
    echo/
    echo Timed Out> InputLine.txt
    
    exit /B
    

    【讨论】:

    • 这有一个依赖,即当前用户对当前目录有RWXD权限。不能保证一定是这样。
    • @BILL 在您的脚本上发出 sed 's/InputLine.txt/"%TMP%"\\InputLine.txt/g' 并解决了,对,假设用户对 %TMP 具有写入权限%,否则将 %TMP% 替换为您希望的任何路径,前提是您使用 sed 进行搜索/替换,您必须使用 '\\' 转义 \。另一种选择是在 notepad.exe 中打开文件并进行搜索和替换。搜索:InputLine.txt 替换:""\InputLine.txt 这些是 Aacini 无法猜测的环境问题,通常很容易解决;你必须承认,他做了最难的部分。
    【解决方案2】:

    如果允许下载工具,您可以使用带有-t 参数的editv64.exe(64 位)或editv32.exe(32 位)。例如:

    editv64 -p "Enter something within 10 seconds: " -t 10 INP
    

    该工具具有其他优点,例如在键入时隐藏输入字符串 (-m) 以及以交互方式编辑现有环境变量。你可以在这里下载:

    http://www.westmesatech.com/editv.html

    【讨论】:

      【解决方案3】:

      下面有一个 Batch-JScript 混合脚本可以实现你想要的。从 XP 开始,每个版本的 Windows 都安装了 JScript。使用 .bat 扩展名保存此文件:

      @if (@CodeSection == @Batch) @then
      
      
      @echo off
      rem Give the desired wait time in milliseconds:
      start "" /B Cscript //nologo //E:JScript "%~F0" 10000
      set "input=Time out"
      set /P "input=You have 10 seconds to complete this input: "
      echo Input read: %input%
      goto :EOF
      
      
      @end
      
      
      WScript.Sleep(WScript.Arguments(0));
      WScript.CreateObject("WScript.Shell").SendKeys("{ENTER}");
      

      【讨论】:

      • 只有当 SendKeys 方法发送 Enter 击键时焦点位于控制台窗口上时,此解决方案才“有效”。因此,这不是一个真正强大的解决方案。
      • @Bill_Stewart 这可能是你能得到的最好的了。
      • @JohnDOE - IMO editv32/editv64 是一个更好、更强大的解决方案。
      • “如果允许可下载的工具”这是您自己的话。 @Aacini 的答案默认有效。这就是为什么它是你能得到的“最好的”。
      • 只有在焦点不变的情况下才有效。因此在许多情况下它是不可靠的。如果您不能使用可下载的工具,那么choicetimeout 命令可能更可取。
      【解决方案4】:

      基本上,你不能。

      您可以使用choice 来接受单个字符超时。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多