Cla*_*diu 119 windows shell scripting batch-file
如何让Windows批处理脚本等待几秒钟?
sleep而wait似乎不工作(不能识别的命令).
ten*_*our 174
你可以试试
ping -n XXX 127.0.0.1 >nul
Run Code Online (Sandbox Code Playgroud)
其中XXX是等待的秒数加一.
Jai*_*oto 156
我不知道为什么这些命令不适合你,但你也可以尝试超时
timeout <delay in seconds>
Run Code Online (Sandbox Code Playgroud)
luk*_*uku 148
timeout /t 10 /nobreak > NUL
Run Code Online (Sandbox Code Playgroud)
/t 指定等待的时间,以秒为单位
/nobreak 按键不会中断超时(CTRL-C除外)
> NUL 将抑制命令的输出
Gol*_*rol 13
要等10秒钟:
choice /T 10 /C X /D X /N
Run Code Online (Sandbox Code Playgroud)
小智 13
Microsoft有一个可以直接调用的睡眠功能.
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
Run Code Online (Sandbox Code Playgroud)
你可以说睡眠1例如在你的批处理脚本中睡1秒钟.
对于这个用例,IMO Ping有点骇人听闻.
对于纯cmd.exe脚本,您可以使用这段代码以hundreths秒为单位返回当前时间.
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
Run Code Online (Sandbox Code Playgroud)
然后,您可以在这样的等待循环中使用它.
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
Run Code Online (Sandbox Code Playgroud)
将所有部分组合在一起:
@echo off
setlocal enableextensions enabledelayedexpansion
call :gettime t1
echo %t1%
call :wait %1
call :gettime t2
echo %t2%
set /A tt = (t2-t1)/100
echo %tt%
goto :eof
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
Run Code Online (Sandbox Code Playgroud)
有关此处使用的命令的更详细说明,请检查HELP SET和HELP CALL信息.
呵呵,Windows 呃……很有趣。这有效:
choice /T 1 /d y > NUL
Run Code Online (Sandbox Code Playgroud)
choice出现一个提示,询问您是或否。/d y让它选择是。/t 1让它在输入之前等待一秒钟。> NUL挤压输出。
我依赖JScript。我有一个像这样的 JScript 文件:
// This is sleep.js
WScript.Sleep( WScript.Arguments( 0 ) );
Run Code Online (Sandbox Code Playgroud)
在批处理文件中,我使用CScript运行它(通常是%SystemRoot%\system32\cscript.exe)
rem This is the calling inside a BAT file to wait for 5 seconds
cscript /nologo sleep.js 5000
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
178410 次 |
| 最近记录: |