如何在批处理文件/ cmd中休眠五秒钟[重复]
本文翻译自:How to sleep for five seconds in a batch file/cmd [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How to wait in a batch script? 如何在批处理脚本中等待? [duplicate] 6 answers [重复] 6个答案
Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after five seconds, such as taking an image being displayed by the webcam. Windows的Snipping工具可以捕获屏幕,但有时我想在五秒钟后捕获屏幕,例如拍摄网络摄像头显示的图像。 (Run the script and smile at the camera, for example.) (例如,运行脚本并对相机微笑。)
How do I sleep for 5 seconds in a batch file? 如何在批处理文件中睡5秒?
#1楼
参考:https://stackoom.com/question/713C/如何在批处理文件-cmd中休眠五秒钟-重复
#2楼
Well this works if you have choice or ping. 如果您有选择或ping,这是有效的。
@echo off
echo.
if "%1"=="" goto askq
if "%1"=="/?" goto help
if /i "%1"=="/h" goto help
if %1 GTR 0 if %1 LEQ 9999 if /i "%2"=="/q" set ans1=%1& goto quiet
if %1 GTR 0 if %1 LEQ 9999 set ans1=%1& goto breakout
if %1 LEQ 0 echo %1 is not a valid number & goto help
if not "%1"=="" echo.&echo "%1" is a bad parameter & goto help
goto end:help
echo SLEEP runs interactively (by itself) or with parameters (sleep # /q )
echo where # is in seconds, ranges from 1 - 9999
echo Use optional parameter /q to suppress standard output
echo or type /h or /? for this help file
echo.
goto end:askq
set /p ans1=How many seconds to sleep? ^<1-9999^>
echo.
if "%ans1%"=="" goto askq
if %ans1% GTR 0 if %ans1% LEQ 9999 goto breakout
goto askq:quiet
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
goto end:breakout
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
echo Slept %ans1% second^(s^)
echo.:end
just name it sleep.cmd or sleep.bat and run it 只需将其命名为sleep.cmd或sleep.bat并运行即可
#3楼
Can't we do waitfor /T 180
? 我们不能waitfor /T 180
吗?
waitfor /T 180 pause
will result in "ERROR: Timed out waiting for 'pause'." waitfor /T 180 pause
将导致“ERROR:超时等待'暂停'。”
waitfor /T 180 pause >nul
will sweep that "error" under the rug waitfor /T 180 pause >nul
将扫描地毯下的“错误”
The waitfor
command should be there in Windows OS after Win95 在Win95之后,Windows操作系统中应该有waitfor
命令
In the past I've downloaded a executable named sleep
that will work on the command line after you put it in your path. 在过去,我已经下载了一个名为sleep
的可执行文件,它可以在您将其放入路径后在命令行上运行。
For example: sleep shutdown -r -f /m \\\\yourmachine
although shutdown now has -t option built in 例如: sleep shutdown -r -f /m \\\\yourmachine
虽然shutdown现在内置了-t选项
#4楼
An improvement of the code proposed by the user Aacini, It has resolution of hundredths of a second and does not fail when the time reaches 23:59:59,99: 用户Aacini提出的代码的改进,它具有百分之一秒的分辨率,并且当时间达到23:59:59,99时不会失败:
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC:: Example delay 1 seg.
set /a TFIN=%TBASE%+100:ESPERAR
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CCif %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL%
if %TACTUAL% lss %TFIN% goto ESPERAR
#5楼
I was trying to do this from within an msbuild task, and choice and timeout both did not work due to I/O redirection. 我试图在msbuild任务中执行此操作,并且由于I / O重定向,选择和超时都不起作用。
I ended up using sleep.exe from http://sourceforge.net/projects/unxutils , which is nice because it doesn't require any install and it's tiny. 我最终使用了来自http://sourceforge.net/projects/unxutils的 sleep.exe,这很不错,因为它不需要任何安装,而且很小。
Trying with choice
: 尝试choice
:
<Target Name="TestCmd"><Exec Command="choice /C YN /D Y /t 5 " />
</Target>
Results in: 结果是:
TestCmd:choice /C YN /D Y /t 5EXEC : error : The file is either empty or does not contain the valid choices. [test.proj][Y,N]?
C:\test.proj(5,9): error MSB3073: The command "choice /C YN /D Y /t 5 " exited with code 255.
Trying with timeout
: 尝试timeout
:
<Target Name="TestCmd"><Exec Command="timeout /t 5 " />
</Target>
Results in: 结果是:
TestCmd:timeout /t 5
EXEC : error : Input redirection is not supported, exiting the process immediately. [test.proj]
C:\test.proj(5,7): error MSB3073: The command "timeout /t 5 " exited with code 1.
Aside: 在旁边:
I am actually using <Exec Command="sleep 2 & dbghost.exe" />
because I am executing dbghost.exe multiple times in parallel and it creates temp files/databases based on the current epoch time in seconds - which of course means if you start multiple instances, each uses the same temp name. 我实际上正在使用<Exec Command="sleep 2 & dbghost.exe" />
因为我并行多次执行dbghost.exe并且它基于当前的纪元时间以秒为单位创建临时文件/数据库 - 这当然意味着你启动多个实例,每个实例使用相同的临时名称。 I was originally trying to use MSBuild Extension Pack Thread.Sleep
command, but it seems that (usually) it was running the sleep task fine, but then starting the <exec>
task in all threads at the same time, and of course dbghost.exe would fail with conflicts. 我原本试图使用MSBuild Extension Pack Thread.Sleep
命令,但似乎(通常)它正在运行睡眠任务,但随后在所有线程中同时启动<exec>
任务,当然还有dbghost。 exe会因冲突而失败。 So far, using sleep.exe seems to be more reliable. 到目前为止,使用sleep.exe似乎更可靠。
#6楼
I made this. 我做的。 It is working and show time left in seconds. 它正在工作,并在几秒钟内显示时间。 If you want to use it, add to a batch file: 如果要使用它,请添加到批处理文件:
call wait 10
It was working when I tested it. 当我测试它时,它正在工作。
Listing of wait.bat
(it must be in the working directory or windir/system32/
): wait.bat
列表(它必须在工作目录或windir/system32/
):
@echo offset SW=00set SW2=00set /a Sec=%1-1set il=00
@echo Wait %1 second
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC, SW=CC set /a TFIN=%TBASE%+%100:ESPERAR
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CC, SW2=CCif %SW2% neq %SW% goto notype
if %il%==0 (echo Left %Sec% second & set /a Sec=sec-1 & set /a il=il+1)
goto no0
:notype
set /a il=0
:no0if %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL%
if %TACTUAL% lss %TFIN% goto ESPERAR
如何在批处理文件/ cmd中休眠五秒钟[重复]相关推荐
- 在CMD中输入adb命令,提示“‘adb‘ 不是内部或外部命令,也不是可运行程序或批处理文件”的解决方法
在android开发中有时我们会用到adb命令行,但很多人在cmd中输入adb命令后,会出现'adb'不是内部或外部命令,也不是可运行程序或批处理文件的问题,如图 解决步骤如下: 打开AndroidS ...
- cmd中的进度如何捕捉到输出内容_python 中日志异步发送到远程服务器
在python中使用日志最常用的方式就是在控制台和文件中输出日志了,logging模块也很好的提供的相应的类,使用起来也非常方便,但是有时我们可能会有一些需求,如还需要将日志发送到远端,或者直接写入数 ...
- 小程序 graphql_GraphQL应用程序中的五个常见问题(以及如何解决)
小程序 graphql by Sacha Greif 由Sacha Greif GraphQL应用程序中的五个常见问题(以及如何解决) (Five Common Problems in GraphQL ...
- 在cmd中使用start运行exe文件闪退问题
如果我们是使用的bat批处理文件来执行某件事,我们可以直接使用/k参数,或者pause命令来让容器(cmd)执行完文件后不退出. 如果我们是在cmd中使用start或其他命令直接执行任务,导致当前容器 ...
- cron 五秒钟_五秒钟规则-它在国际上适用吗?
cron 五秒钟 There's a good article in the Washington Post from July that was on a repeat on the Radio t ...
- 在cmd中运行.java文件
一.检查环境 检查java环境是否配置成功,输入javac和java -v.出现下图结果就是配置好了. 二.在记事本编写java 代码 public class HelloWorld {public ...
- cmd中执行netsh wlan start hostednetwork 无法启动承载网络。 组或资源的状态不是执行请求操作的正确状态。
cmd中执行netsh wlan start hostednetwork 无法启动承载网络. 组或资源的状态不是执行请求操作的正确状态. 解决办法: 1.打开网络共享中心,点击"更改适配器& ...
- 在cmd中输入javac不行_cmd运行java可以javac不行
如果javac无法运行的话,只能说明配置是成功完成,但是并不是配置正确的.如:jdk安装在"d:\programfiles\jdk1.7.0_10"第一步:新建"java ...
- Windows 7的CMD中 Telnet 无法执行的解决办法
在Windows 7的CMD中,执行telnet 192.168.1.10 80 会提示没有这个命令的提示,这是因为Win7默认是没有安装Telnet的(在以往的WINDOWS系统中都是默认安装的). ...
最新文章
- 0525 项目回顾7.0
- 多层AOP 解决AOP执行顺序
- 华为SAN存储在linux下的,多路径 - 华为SAN存储在SUSE系统下的主机连通性指南 - 华为...
- terminating with uncaught exception of type std::bad_cast: std::bad_cast
- 如何使用html和css,如何使用html和css制作这个div?
- js基本数据类型和复杂数据类型的区别
- 面试题系列(10):一个大型电商网有大量的图片,加载很慢,你有哪些方法优化这些图片的加载?...
- 【Python】循环的拓展
- linux端口接收中文乱码,linux中显示中文乱码如何解决
- DICOM开发工具总结
- 街机三国服务器维护,街机三国4月2日07:00更新维护公告
- Floppy Zip Disk Rescue注册码分析
- “飞思卡尔”杯全国大学生智能车竞赛
- html转word 自动分页,word怎样自动分页
- linux 多个ftp站点,vsftp在虚拟主机上建立多个ftp站点
- 9V降压5V低功耗恒压稳压芯片,大电流3A方案和LDO
- 使用 Python 制作黑白照片生成器
- 最新手机语音助手的调研
- 深圳礼品展上新“缤纷食品馆” 解锁员工福利新场景
- 求空间两条直线之间的距离