windows 打开程序的批处理文件等待 5 秒然后关闭该程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22046494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
batch file that opens a program waits 5 seconds then closes that program
提问by cheapkid1
For some reason I cannot get this batch to perform the open portion, wait, then the close portion in Windows XP or Windows 7. I can only open or close by commenting out the other in the batch, but for some reason how I have it written it will only open. How can I open a program wait a fixed time, then close that same program? The reason for doing this is to force a manufacturer program's drivers when the computer starts. Any help would be much appreciated. Thanks.
出于某种原因,我无法让这个批处理执行打开部分,等待,然后在 Windows XP 或 Windows 7 中关闭部分。我只能通过注释掉批处理中的另一个来打开或关闭,但出于某种原因,我是如何拥有它的写了它只会打开。如何打开程序等待固定时间,然后关闭同一个程序?这样做的原因是在计算机启动时强制制造商程序的驱动程序。任何帮助将非常感激。谢谢。
@ECHO off
"C:\Program Files\WinTV\WinTV2K.EXE" -nc
Set _Delay=5
Taskkill /IM "WinTV2K.EXE" /F
Exit
回答by foxidrive
Instead of
代替
Set _Delay=5
use this
用这个
ping -n 6 localhost >nul
The ping command requires 6 to give you an approximate delay of 5 seconds.
ping 命令需要 6 次才能给您大约 5 秒的延迟。
回答by cheapkid1
Thanks guys for your help with the wait ping, that was a great fix for the timer, however the only way I could get the batch to work was to place the batch file inside the program directory and run it like this:
感谢大家对等待 ping 的帮助,这对计时器来说是一个很好的修复,但是我能让批处理工作的唯一方法是将批处理文件放在程序目录中并像这样运行它:
@ECHO off
start WinTV2K.EXE -nc
ping 1.1.1.1 -n 1 -w 5000 > nul
Taskkill /IM WinTV2K.EXE /F
Exit
Not sure why it wouldn't work the other way, and it's not exactly what I wanted, but it will do. Thanks for all your help. @Ryan and @Sean Long
不知道为什么它不会以其他方式工作,这不完全是我想要的,但它会做。感谢你的帮助。@Ryan 和 @Sean Long
回答by Sean Long
回答by Ryan
Instead of Set _Delay=5
, use the following:
代替Set _Delay=5
,使用以下内容:
ping 1.1.1.1 -n 1 -w 5000 > nul
ping 1.1.1.1 -n 1 -w 5000 > nul
Will ping 1.1.1.1
once (-n 1
) and wait 5 seconds for a response (-w 5000
)
将 ping1.1.1.1
一次 ( -n 1
) 并等待 5 秒以获得响应 ( -w 5000
)
As the value is outputted to nul
it won't affect your program other than waiting for 5 seconds.
由于该值被输出到nul
它除了等待 5 秒之外不会影响您的程序。
回答by Ryan
This will open your program wait 5 sec., then close that same program timeout for Windows XP - 10 "batch file" or "Windows Command Script"! no ping!!!
这将打开您的程序等待 5 秒,然后关闭 Windows XP 的相同程序超时 - 10“批处理文件”或“Windows 命令脚本”!没有平!!!
@ECHO off
start WinTV2K.EXE -nc
ver | find "XP" > nul
if %ERRORLEVEL% == 1 timeout /t 05 && echo . Vista - 10 && goto NEXT
set XP-timeout=5000
echo . XP, 1000 = 1 sec, approximately
set /a timeout=1
:timer
echo timeout timer number %timeout%
if %timeout% == %XP-timeout% goto NEXT
echo . XP, 1000 = 1 sec.
set /a timeout=timeout+1
goto timer
:NEXT
tasklist /fi "imagename eq WinTV2K.EXE" |find ":" > nul
if errorlevel 1 taskkill /f /im "WinTV2K.EXE"
Exit