Batch File auto quits. I can't find the error - batch-file

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

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
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.

Related

Loop a batch file only 5 times

Ive been trying to loop a batch file exactly 5 times using the set /a operation and so far no luck! Could someone help me?
#ECHO OFF
set loopCount=0
:loop
echo Checked %loopCount% times...
set /a loopCount=1+%loopCount%
if loopCount == 5 (goto exit) else (goto loop)
:exit
cls
echo Finished after %loopCount% times
pause >nul
Best of luck to whoever can help me
Here's a couple of examples to help you.
Using GoTo with a label:
#Set "loopCount=0"
:loop
#Set /A loopCount += 1
#Rem Some actual command goes here.
#Echo Checked %loopCount% times...
#If %loopCount% Lss 5 GoTo loop
#%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
#ClS
#Echo Finished after being checked %loopCount% times.
#Pause 1>NUL
Alternatively, using a For /L looping mechanism:
#Set "maxCount=5"
#For /L %%G In (1,1,%maxCount%) Do #(
Rem Some actual command goes here.
Echo Checked %%G times...
)
#%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
#ClS
#Echo Finished after being checked %maxCount% times.
#Pause 1>NUL
If you need to execute the entire batch script 5 times, the following method of storing an execution count in an alternate data stream of the script can be used:
#Echo off
more < "%~f0:Count" 2>&1 > nul && (
For /f "usebackq delims=" %%G in (`more ^< "%~f0:Count"`)Do Set /A Count=%%G+1
) || (
Set /A "Count+=1"
)
Echo(%Count% >"%~f0:Count"
:# Your script below
Echo(Script execution: %Count%
:# Terminate script execution after count reached
If %Count% EQU 5 (
Echo(0 >"%~f0:Count"
Exit /B
)
"%~f0"

How to see whether or not a specifed program has a file open with Batch

Recently I have been building a rather small piece of batch coding to be run in the background using someone elses VBS. What I have been trying to get it to do is (on weekdays, between two set times) open an excel file named "Homework.xlxs"(if it wasn't already running). Then I ran into a problem as I realised that the method I am currently using doesn't check for a specified file or whether or not that file is open. This means that I could be simply fiddling around with Excel and have my code not run the way I would want it to.
Here is my current code:
#ECHO off
SET "stime=16:00:00.00"
SET "etime=17:00:00.00"
:start
FOR /F %%F IN ('wmic path win32_localtime get dayofweek^|Findstr [0-6]') DO SET DOW=%%F
tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
if "%ERRORLEVEL%"=="0" (
SET "running=true"
) ELSE (
SET "running=false"
)
IF %time% GEQ %stime% IF %time% LEQ %etime% IF %running%==false IF %DOW% NEQ 6 IF %DOW% NEQ 0(
START /MAX C:\Users\Hunter\Desktop\Homework\Homework.xlsx
)
TIMEOUT 60 /NOBREAK > NUL
GOTO start

Writing a batch file to detect ping anomalies

I'm trying to create a batch file where it would detect ping anomalies. I want it to ping to an IP infinitely (-t) until I close it where it would write down whenever ms > 100 ms and the time stamp as well. I'm thinking somewhere along the lines of sub string variables but I don't know how to wrap my head around it.
:Loop
time /t >> textfile.txt
ping -n 1 127.0.0.1 | findstr /c:"Minimum" >> textfile.txt
timeout /t 5
Goto Loop
Or perhaps this suits your needs
ping /t > textfile.txt
or
:loop
wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord
goto loop
I've been looking for an answer to this same question for while. bgalea's answer gave me the pieces I needed to write my own. Here's what I came up with:
:: usage: badpings.bat [ip adress | hostname] [ping time threshhold]
#echo off
if "%1"=="" (
set pingdest=yahoo.com
) else (
set pingdest=%1
)
if "%2"=="" (
set /a limit=100
) else (
set /a limit=%2
)
echo Pinging %pingdest%.
echo Logging replies over %limit%ms.
echo Press Ctrl+C to end.
:Loop
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
set var=%%a %%b %%c %%d %%e %%f
set pingtimestr=%%e
)
if "%pingtimestr%"=="find" (
echo Ping request could not find host %pingdest%. Please check the name and try again.
goto End
)
if "%pingtimestr%"=="host" (
set /a pingtime=%limit%+1
)
if "%pingtimestr:~0,4%"=="time" (
set /a pingtime=%pingtimestr:~5,-2%
)
if %pingtime% GTR %limit% (
echo [%time%] %var%>>badpings.log
echo [%time%] %var%)
timeout /t 1 /nobreak >nul
Goto Loop
:End
It works on Windows 10. I haven't tested it on other OS versions.

Combination of `sleep` and `pause` command

Ok, after seeing crazy stuff being completed in so little code, I have high hopes this is possible.
Pretty much, I want to use the pause command normally, however, if the user doesn't input anything for a specified duration of time, it automatically continues.
In pseudo code:
(sleep %sleep-time%&Echo Pass)1>0 & pause
I thought at first I could do this using start /b to create a process that echoed input while being paused i the current thread, but that could cause problems if the user does input something.
Bonus
What would be really cool is if the errorlevel would be changed based on whether the user inputted something, or if the pause command timed out.
I suggest using timeout:
timeout /T 60 >NUL
This will sleep your script for 1 minute, or unless the user hits a key.
#echo off
setlocal
rem TimedPause.bat - Antonio Perez Ayala
if "%1" equ ":PausePart" goto PausePart
if "%1" neq "" goto begin
echo TimedPause.bat seconds
echo/
echo Wait for given seconds or until user press a key
echo At end, the presence of keyPressed.txt file indicate the cause of exit
goto :EOF
:begin
set seconds=%1
start "" /B "%~F0" :PausePart
for /F "skip=2 tokens=2 delims=," %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO CSV') do (
set PausePart=%%a
goto TimePart
)
:TimePart
ping -n 2 localhost > NUL
if exist keyPressed.txt goto :EOF
set /A seconds-=1
if %seconds% gtr 0 goto TimePart
taskkill /PID %PausePart% /F > NUL
goto :EOF
:PausePart
del keyPressed.txt 2> NUL
pause
echo %time% > keyPressed.txt
exit

Batch Loop with Timer

Hi I'm new to batch and I am running into a issue. What i'm looking to do is to write a batch file that will run a command test.exe over and over for 24 hours. Once the 24 hour timer expire, the program will exit.
I am not familiar with batch and was wondering how this can be done.
Try this code below. Using ping as timer and hiding it in the background using find switches.
You can modify timer by playing with ping command and to exit play with %n% variable.
This code re-runs test approx every 30 minutes and runs only 4 times
#Echo off
SET n=0
:Loop
SET /A n=n+1
test.exe
Ping 1.1.1.1 -n 1000 -w 1 | find /V "Request timed out" | find /V "Ping" | find /V "Packets"
if %n% EQU 4 (
exit
) Else if %n% LEQ 3 (
Goto Loop
)
Use a label in your batch file to create a loop. Inside the loop, execute the test.exe.
For the 24-hour execution period, I believe you can create a scheduled task to start and stop the execution. I am not sure how graceful the exit will be once the scheduled task will end.
Dunno if a double else is feasable... never tried... but here's my shot.
#Echo off
FOR /F "tokens=1" %%F IN ('ECHO %date%') DO (
set day=%%F
)
FOR /F "tokens=* delims=:." %%F IN ('ECHO %time%') DO (
set thetime=%%F
)
:Loop
START /B "test.exe"
FOR /F "tokens=1" %%F IN ('ECHO %date%') DO IF %date%==%day% IF %time% GEQ %thetime% (
GOTO Loop
) Else (
IF NOT "%date%"=="%day%" IF %time% LEQ %thetime% (
GOTO Loop
) ELSE (
IF NOT "%date%"=="%day%" IF %time% GRT %thetime% (
GOTO:EOF
)
)

Resources