批处理文件自动退出.我找不到错误 [英] Batch File auto quits. I can't find the error
问题描述
我一直在编码一件很小的东西,这很愚蠢,已经花了我一段时间.它的意思是总是在两个特定的时间(4:30和5:30)之间打开一个excel电子表格(名为Homework.xlsx).问题是,即使我将其设置为每分钟重复一次的循环,实际上并没有开放检查那些时间. 这是代码:
I have been coding a small thing that, rather stupidly, has been taking me a while. What it is meant to do is always have a certain excel spreadsheet (named Homework.xlsx) open between two certain times (4:30 and 5:30). The thing is it doesn't actually stay open to check for those times, even though I have it set to a loop that repeats every minute. Here is the code:
SET "stime=16:30:00.00"
SET "etime=17:30:00.00"
:start
TASKLIST /FI "IMAGENAME EQ EXCEL.EXE" 2>NUL | FIND /I /N "EXCEL.EXE" >NUL
IF %ERRORLEVEL% = 0 (
SET Erunning=true
) ELSE (
SET Erunning=false
)
IF EXIST C:\Users\Hunter\Documents\HOMEWORKSHEETISOPEN.txt (
IF %Erunning%=false (
DEL C:\Users\Hunter\Documents\HOMEWORKSHEETISOPEN.txt
)
)
IF EXIST C:\Users\Hunter\Documents\HOMEWORKSHEETISOPEN.txt (
SET running=true
) ELSE (
SET running=false
)
FOR /F %%F IN ('wmic path win32_localtime get dayofweek^|Findstr [0-6]') DO SET DOW=%%F
IF %time% GEQ %stime% IF %time% LEQ %etime% IF %running%==false IF %DOW% NEQ 6 IF %DOW% NEQ 0 (
START /MAX C:\Users\Hunter\OneDrive\Homework.xlsx
ECHO IMRUNNING > C:\Users\Hunter\Documents\HOMEWORKSHEETISOPEN.txt
)
TIMEOUT 60 /NOBREAK > NUL
GOTO start
推荐答案
您的代码不仅令人费解,而且效率低下.如果目的是打开Monday..Friday @ %stime%...%etime%
范围内的excel电子表格,那么有更简单的方法可以做到这一点.例如:
Your code is not just convoluted, but also inefficient. If the purpose is open an excel spreadsheet in Monday..Friday @ %stime%...%etime%
range, then there are simpler ways to do that. For example:
SET "stime=16:30:00.00"
SET "etime=17:30:00.00"
:start
rem Check the day
FOR /F %%F IN ('wmic path win32_localtime get dayofweek^|Findstr [0-6]') DO SET DOW=%%F
IF %DOW% EQU 0 GOTO wait1hour
IF %DOW% EQU 6 GOTO wait1hour
rem Check the time
IF %time% LSS %stime% GOTO wait1min
IF %time% GTR %etime% GOTO wait1min
rem Open the spreadsheet and wait until it is closed
START /MAX C:\Users\Hunter\OneDrive\Homework.xlsx | PAUSE > NUL
GOTO start
:wait1min
TIMEOUT 60 /NOBREAK > NUL
GOTO start
:wait1hour
TIMEOUT 3600 /NOBREAK > NUL
GOTO start
如果%time%
检查失败,您还可以计算要等待的确切秒数,因此wait方法更加有效;我建议您使用此方法这样的计算.您也可以使用Windows Task Scheduler在所需的时间在星期一至星期五范围内启动程序.
You may also calculate the exact number of seconds to wait if the %time%
check fail, so the wait method be more efficient; I suggest you to use this method for such a calculation. You may also start the program in Mon..Fri range at the desired hour using the Windows Task Scheduler.
这篇关于批处理文件自动退出.我找不到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!