2015-04-02 71 views
0

我一直在试图创建一个文件,用户的输入。 我能得到这个文件Set/p即时

start /min b1.bat 
(:b1.bat) 
@echo off 
cls 
set num=0 
:time 
set /a num=%num%+1 
echo %num% >waiter.txt 
set time=0 
:wait 
cls 
set /a time=%time%+1 
if "%time%" equ "1000" goto time 
goto wait 

这里,b1.bat计数通过secconds,因为它已经开始。

(:b2.bat) 
cls 
:begin 
set /p time= <waiter.txt 
echo %time%  
if "%time%" equ "5" echo Five secconds. 
set /p cho=Input: 
goto begin 

我想知道如何使b2.bat做一些事情,如果没有输入的时间量。根本没有输入,甚至没有输入。

因此,如果我完全没有按任何按键并且一分钟过去了,它会打印一分钟而没有输入。

我希望这对你有意义。

回答

0

在我回答你的问题之前,我需要指出循环1000次并不一定意味着你已经暂停1秒。有更好的睡眠方式 - 在Vista或更新版本中为timeout /t seconds,或在XP中为ping -n seconds+1 localhostchoice /t seconds。你也应该避免跺脚existing environment variables,比如%time%

要回答你的问题,没有优雅的方法来设置set /p的超时时间。但是,您可以使用start /b在同一窗口内启动后台线程以在一个时间间隔内回显内容。您可以使用waitfor(Vista或更新版本)来设置活动线程和后台之间的线程间通信来重置计时器,并在waiter.txt消失时将辅助线程设置为自毁。

只要你在同一个窗口内启动一个助手线程,不妨把你的waiter.txt线程放在那里。为什么产生一个最小化的窗口,当你可以重复使用已有的窗口?而且由于我们在同一个窗口中合并了进程线程,所以还可以更进一步,并将所有脚本合并到一个脚本中,以便为不同的线程重新启动不同的参数。

@echo off 
setlocal 

if "%~1"=="" (
    >waiter.txt echo 0 
    start /b "" "%~f0" helper1 
    start /b "" "%~f0" helper2 
) else goto %~1 

:begin 
cls 
set /p "seconds="<waiter.txt 
echo Alive for %seconds%s. 

:read-host 
set "cho=" 
set /p "cho=" 
if not defined cho goto read-host 

rem // delayed expansion prevents evaluation of special characters in user input 
setlocal enabledelayedexpansion 
for %%c in (quit exit die EOL) do if /i "!cho!"=="%%c" (
    del waiter.txt 
    echo OK, bye! 
) 
rem // send signal to helper2 acknowledging entry 
waitfor /s %computername% /si UserEntry >NUL 2>NUL 
if not exist waiter.txt exit /b 

rem // ======================================== 
rem // Do something meaningful with !cho! here. 
rem // ======================================== 
endlocal 

goto begin 

rem // formerly b1.bat 
:helper1 
set "num=0" 
:helper1_loop 
timeout /t 1 /nobreak >NUL 2>NUL 
set /a num+=1 
if not exist waiter.txt exit 
>waiter.txt echo %num% 
goto helper1_loop 

:helper2 
if not exist waiter.txt exit 
waitfor /t 60 UserEntry >NUL 2>NUL || (
    echo A minute has passed without input. 
) 
goto helper2 

对于额外的信用,让我们摆脱waiter.txt的为好。我不确定这种偏执狂是否成立,但我相信硬盘领域可以忍受有限数量的写作。每秒钟重写waiter.txt可能会影响我的工作。因此,让我们创建一个混合Jscript块来计算脚本启动和现在之间所经过的秒数。

要控制后台助手线程的执行,我们仍然会使用一个临时文件,但我们将使用一个lock file,它将只写入一次,然后在最后删除。

@if (@CodeSection == @Batch) @then 

@echo off 
setlocal 

if "%~1"=="" (
    rem // relaunch self as helper if lock file is writeable 
    2>NUL (>"%~dpn0.lock" type NUL) && start /b "" "%~f0" helper 

    rem // create a lock file for the duration of the main runtime 
    rem // Credit: Dave Benham -- https://stackoverflow.com/a/27756667/1683264 
    8>&2 2>NUL (2>&8 9>"%~dpn0.lock" call :init) || (
     echo Only one instance is allowed. 
     timeout /t 3 /nobreak >NUL 
     exit /b 
    ) 
) else goto %~1 

rem // cleanup and end main runtime 
del "%~dpn0.lock" >NUL 2>NUL 
waitfor /s %computername% /si UserEntry >NUL 2>NUL 
exit /b 

:init 
rem // set %start% to current epoch 
call :jscript start 

:begin 
cls 
call :jscript seconds 
echo Alive for %seconds%s. 

:read-host 
set "cho=" 
set /p "cho=" 
if not defined cho goto read-host 

rem // delayed expansion prevents evaluation of special characters in user input 
setlocal enabledelayedexpansion 
for %%c in (quit exit die EOL) do if /i "!cho!"=="%%c" (
    echo OK, bye! 
    exit /b 
) 
rem // send signal to helper2 acknowledging entry 
waitfor /s %computername% /si UserEntry >NUL 2>NUL 

rem // ======================================== 
rem // Do something meaningful with !cho! here. 
rem // ======================================== 
endlocal 

goto begin 

:helper 
del "%~dpn0.lock" >NUL 2>NUL 
if not exist "%~dpn0.lock" exit 
waitfor /t 60 UserEntry >NUL 2>NUL || (
    echo A minute has passed without input. 
) 
goto helper 

:jscript 
for /f %%I in ('cscript /nologo /e:JScript "%~f0" "%start%"') do set "%~1=%%I" 
goto :EOF 

@end // end batch/begin JScript hybrid code 
WSH.Echo(
    WSH.Arguments(0) 
    ? Math.round((new Date() - WSH.Arguments(0))/1000) 
    : new Date().getTime() 
);