2015-04-02 63 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() 
);