I'm new to this so try to keep my question relevant! I'm trying to write a batch file that will re-start an application on a 2 state condition i.e. when a ping fails and then recovers. I've written something gleaned from information given on this site (see below) that just restarts it when it fails and changes the DOS prompt colour, but it's not ideal. Can anyone point me in the general direction to restart the app on a down -> up condition with a ping? Many thanks!
#echo off
cls
set INTERVAL=120
:top
ping -n 1 -w 2000 192.168.1.10 | find "TTL="
IF ERRORLEVEL 1 (SET OUT=4F & echo Request timed out.) ELSE (SET OUT=2F)
IF ERRORLEVEL 1 goto reset
COLOR %OUT%
ping -n 2 -l 100 127.0.0.1 >nul
goto top
:reset
timeout %INTERVAL%
taskkill /IM VmsClientApp.exe /F
Ping -n 1 -l 256 127.0.0.1 >nul
start /D "c:\Program Files\Avigilon\Avigilon Control Center Client\" VmsClientApp.exe
echo The Client is now loading...
goto top
#echo off
setlocal enableextensions disabledelayedexpansion
set "interval=120"
cls
:up
color 2f
set "down="
:test
ping -n 1 -w 2000 192.168.1.10 | find "TTL="
if not errorlevel 1 if defined down goto :restart
if errorlevel 1 set "down=1" & color 4f
timeout %interval%
goto :test
:restart
taskkill /IM VmsClientApp.exe /F
start "" /D "c:\Program Files\Avigilon\Avigilon Control Center Client" VmsClientApp.exe
goto :up
Related
Having a latency less than 600 but greater than 90 continues to go to :FAST CONNECTION. It should be going to :MODERATE CONNECTION.
#ECHO off
MODE CON:cols=38 lines=11
:LOOP
SET a=3000
FOR /f "tokens=7 delims== " %%G IN ('PING -4 -n 1 8.8.8.8^| FIND "TTL" ') DO SET a=%%G
CLS
IF %a% EQU 3000 (GOTO :NO CONNECTION) ELSE (GOTO :SPEED)
:SPEED
IF %a% GTR 600 (GOTO :SLOW CONNECTION)else (IF %a% LSS 90 (GOTO :FAST CONNECTION) else (GOTO :MODERATE CONNECTION))
TIMEOUT /T 2 > NUL
:NO CONNECTION
COLOR 4F
ECHO.
ECHO.
ECHO --- NO CONNECTION ---
ECHO.
ECHO CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
CLS
ECHO.
ECHO.
ECHO *** NO CONNECTION ***
ECHO.
ECHO CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
CLS
ECHO.
ECHO.
ECHO !!! NO CONNECTION !!!
ECHO.
ECHO CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
GOTO :END
:FAST CONNNECTION
COLOR 2F
ECHO.
ECHO YOU CURRENTLY HAVE A
ECHO FAST CONNECTION TO INTERNET: %a%
ECHO.
ECHO APPLICATIONS AND FILE TRANSFERS
ECHO WILL RUN AT A GREAT RATE
ECHO.
ECHO FAST 0 - 10
ECHO MODERATE 11 - 20
ECHO SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END
:MODERATE CONNECTION
COLOR 6F
ECHO.
ECHO YOU CURRENTLY HAVE A
ECHO MODERATE CONNECTION TO THE INTERNET : %a%
ECHO.
ECHO APPLICATIONS AND FILE TRANSFERS
ECHO WILL RUN AT A SO/SO RATE
ECHO.
ECHO FAST 0 - 10
ECHO MODERATE 11 - 20
ECHO SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END
:SLOW CONNECTION
COLOR 4F
ECHO.
ECHO YOU CURRENTLY HAVE A
ECHO SLOW CONNECTION TO INTERNET: %a%
ECHO.
ECHO ALTERNATE OR CONTINGENCY
ECHO NETWORK WILL RUN AT A SLOWED RATE
ECHO.
ECHO FAST 0 - 10
ECHO MODERATE 11 - 20
ECHO SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END
:END
GOTO :LOOP
FOR /f "tokens=7 delims== " %%G IN ('PING -4 -n 1 8.8.8.8^| FIND "TTL" ') DO SET a=%%G
On a US-EN machine a gets set to a number with the time unit ms appended e.g. 100ms.
This causes the following comparisons to work as string comparisons (lexicographically), not number comparisons, so for example:
C:\etc>if 100ms LSS 11 #echo ???
???
C:\etc>if 100ms LSS 10 #echo ???
C:\etc>
The quick-and-dirty solution in this case is to strip the ms suffix right after the for loop.
set "a=%a:~0,-2%"
Note: this may not - and will likely not - work on localized Windows other than US-EN.
For the purpose of this demonstration, I've set a timeout of 3 seconds at the end of the script, you can adjust this as you please.
Your main issue is that you are using labels with spaces, you cannot do that. Second issue is that, as already mentioned by #Mofi in a comment, depending on your keyboard settings (language) different items are assigned to %a%.
I however suggest that you do 2 pings and use the average result, instead of ttl.
As a side note, ping (using ICMP) is for diagnostics and will not always get priority.
#echo off
MODE CON:cols=38 lines=11
:test
set latency=3000
#echo off
for /f "tokens=2delims==, " %%i in ('ping -4 -n 2 8.8.8.8^| find /i "average" ') do set "latency=%%i"
set latency=%latency:ms=%
if %latency% gtr 600 if %latency% lss 3000 (
color 4F
echo(
echo Slow connection %latency%
echo(
)
if %latency% geq 3000 (
color 4F
echo(
echo Connection seems down %latency%
echo(
)
if %latency% lss 90 (
color 2F
echo(
echo Fast Connection %latency%
echo(
)
if %latency% gtr 90 if %latency% lss 600 (
color 6F
echo(
echo Moderate Connection %latency%
echo(
)
(timeout /t 3)>nul 2>&1 & cls & goto test
You will note, for obvious reasons I removed some of the noise you had, you can add all the echo( back if you want, but I do not see the purpose of it. I also removed all the goto labels, as it is not required.
Please begin by taking note of my comment, which tells you that this code will not provide a measurable indication of your internet connection's speed.
Now, without radically changing your script and layout, you could probably use wmic with Win32_PingStatus, instead on ping. This should ensure that your variable always has the response time in milliseconds, if one is returned within the allowed timespan:
Example, (Just change the Host Address on line 5 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Mode CON:Cols=45 Lines=11
Set "HA=8.8.8.8"
:Loop
Color
Set "RT="
For /F EOL^=R %%G In ('%__AppDir__%wbem\WMIC.exe Path Win32_PingStatus Where^
"Address='%HA%'" Get ResponseTime 2^>NUL')Do For %%H In (%%G)Do Set "RT=%%H"
If Not Defined RT GoTo None
If %RT% Gtr 600 GoTo Slow
If %RT% Gtr 90 GoTo Moderate
:Fast
Color 2F
Echo=
Echo YOU CURRENTLY HAVE A
Echo FAST CONNECTION TO INTERNET: %RT%ms
Echo=
Echo APPLICATIONS AND FILE TRANSFERS
Echo WILL RUN AT A GREAT RATE
Echo=
Echo FAST : 0 - 10
Echo MODERATE : 11 - 20
Echo SLOW : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop
:Moderate
Color 6F
Echo=
Echo YOU CURRENTLY HAVE A
Echo MODERATE CONNECTION TO THE INTERNET: %RT%ms
Echo=
Echo APPLICATIONS AND FILE TRANSFERS
Echo WILL RUN AT A SO/SO RATE
Echo=
Echo FAST : 0 - 10
Echo MODERATE : 11 - 20
Echo SLOW : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop
:Slow
ClS
Color 4F
Echo=
Echo YOU CURRENTLY HAVE A
Echo SLOW CONNECTION TO INTERNET: %RT%ms
Echo=
Echo ALTERNATE OR CONTINGENCY NETWORK
Echo WILL RUN AT A GREAT RATE
Echo=
Echo FAST : 0 - 10
Echo MODERATE : 11 - 20
Echo SLOW : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop
:None
ClS
Color 4F
Echo=
Echo --- NO CONNECTION ---
Echo=
Echo CHECK YOUR NETWORK CONNECTION
Echo=
%__AppDir__%timeout.exe /T -1
GoTo Loop
Please be aware, that I do not know why you've asked the code to loop back to the ping, so if that's not your overall intention, for now you'll need to use CTRL+C to stop if from looping.
I am working on setting up a LAN ping test using a batch file. The code i have works great for websites but it acts strange for local IPs. I am running the ping test on 3 computers that i know the IPs of. No matter which one i unplug, when i run the code below, the %errorlevel% is always 0 on all three computers. It never equals to 1 like it does on a website. How can i resolve this?
#echo off
cls
Set IPaddress=www.google.com
PING %IPaddress% -n 1
call :PingTest
Set IPaddress=www.yahoo.com
PING %IPaddress% -n 1
call :PingTest
Set IPaddress=www.unabletoping.com
PING %IPaddress% -n 1
call :PingTest
pause > null
exit
:PingTest
IF %errorlevel% EQU 1 (echo "Server is Offline") else (GOTO:EOF)
When you ping an non accesible address in your subnet, you get an "unreachable" answer, with 1 packet sent, 1 packed received, 0 packets lost. Errorlevel is not set.
When you ping an non accesible address out of your subnet, you get a "timeout" answer, with 1 packet sent, 0 packet received, 1 packet lost. Errorlevel is set.
And, you can ping an active machine, lost packets and get an errorlevel
And, you can ping an active/inactive machine, get TTL expired and get no errorlevel
Better, check for content of ping response.
ping -n 1 192.168.1.1 | find "TTL=" >nul
if errorlevel 1 (
echo host not reachable
) else (
echo host reachable
)
While I cannot replicate your issue, I do have a few recommendations for your script. (See my comment for questions regarding the issue)
When creating variables encapsulate the scope. setlocal and endlocal
When exiting a script, use the /b flag as to not kill the caller's command prompt.
nul not null.
Example ():
#echo off
setlocal
cls
set "IPaddress=www.google.com"
call :PingVerbose "%IPaddress%"
call :PingVerbose "www.yahoo.com"
call :PingVerbose "www.microsoft.com"
pause>nul
endlocal
exit /b 0
:Ping <Address>
ping "%~1" -n 1 >nul
exit /b %ErrorLevel%
:PingVerbose <Address>
call :Ping %1 && echo %~1 is Online || echo %~1 is Offline
exit /b %ErrorLevel%
Though I also cannot replicate your issue, and too have a suggestion to better your script -
#echo off & cls
set addresses=10.1.1.666 10.124.412.14 10.7.254.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || echo %%a is offline
Note that the command after || will only be executed if an error level is set by the ping.
Taking what others have mentioned, I wanted to show how one may need to do everything shown above plus the use of modified variables such as a counter in a loop.
Note: using "setlocal enabledelayedexpansion" allows the use of modified variables in a loop etc..
#echo off
setlocal enabledelayedexpansion
REM List of systems to check
set list=www.google.com www.yahoo.com www.notasite.com
set /a failed=0
set /a passed=0
set /a count=0
echo PingTest Servers on %list% :
(for %%a in (%list%) do (
set /a "count+=1"
call :PingVerbose %%a && set /a "passed=!passed!+1" || set /a "failed=!failed!+1"
))
echo ------------
echo Result: %passed% of %count% systems are pingable
pause
endlocal
exit /b 0
:Ping <Address>
ping "%~1" -n 1 >NUL
exit /b %ErrorLevel%
:PingVerbose <Address>
call :Ping %1 && echo %~1 - [ONLINE] || echo %~1 - [OFFLINE]
exit /b %ErrorLevel%
Output:
PingTest Servers on www.google.com www.yahoo.com www.notasite.com :
www.google.com - [ONLINE]
www.yahoo.com - [ONLINE]
www.notasite.com - [OFFLINE]
------------
Result: 2 of 3 systems are pingable
Press any key to continue . . .
I'm creating a simple bat file that plays some ASCII animation stored in 120 .txt files. My script works but it works too fast. I'd like to find a way to slow it dow a bit. I've tried the TIMEOUT 1 command but it only plays 1 picture per second which is too slow. Is there a workable solution without moving from Windows 7 command line?
This is my script so far
#echo off
MODE CON: COLS=91 LINES=41
cls
:3
setlocal enableextensions enabledelayedexpansion
FOR /R %%i in (*.txt) do (type "%%i"
)
goto :3
You could use a FOR /L loop to introduce a delay. Here is a script that introduces an approximate 100 msec delay. A simple test near the top computes how many iterations are required to approximate 100 msec. The number will vary between machines. Adjust the definition of msecDelay as required to get your desired result.
#echo off
setlocal
:: Compute the number of iterations required to get the desired delay
set msecDelay=100
set ticks=100000
set "start=%time%"
for /l %%N in (1 1 %ticks%) do rem
set "stop=%time%"
for /f "tokens=3,4 delims=:.," %%A in ("%start%") do set /a start=1%%A%%B-10000
for /f "tokens=3,4 delims=:.," %%A in ("%stop%") do set /a stop=1%%A%%B-10000
if %start% gtr %stop% set /a stop+=6000
set /a delay=msecDelay*ticks/(stop-start)/10
MODE CON: COLS=91 LINES=41
cls
:3
FOR /R %%i in (*.txt) do (
type "%%i"
for /l %%n in (1 1 %delay%) do rem
)
goto :3
I'm wondering if you will get better results by moving CLS within the loop, just before your TYPE statement.
This is a very simple way to slow between frames
Example:
#echo exiting script
ping localhost -n 2 > nul
cls
#echo exiting script .
ping localhost -n 2 > nul
cls
#echo exiting script ..
ping localhost -n 2 > nul
cls
#echo exiting script ...
ping localhost -n 2 > nul
cls
#echo exiting script ....
ping localhost -n 2 > nul
cls
#echo exiting script .....
ping localhost -n 5 > nul
cls
Not built in, but still command line: sleep.exe from Windows Resource Kit (available here: http://www.microsoft.com/en-us/download/details.aspx?id=17657)
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
I want to make a batch file that waits for a few minutes, then executes a command. How would I do this? Specifically, I want it to end another program in 3 minutes after opening the file.
Use timeout. That will let you wait for a number of seconds given in it's /t parameter. timeout /t 180 will sleep for 3 minutes (180 seconds).
TIMEOUT [/T] timeout [/NOBREAK]
Description:
This utility accepts a timeout parameter to wait for the specified
time period (in seconds) or until any key is pressed. It also
accepts a parameter to ignore the key press.
Parameter List:
/T timeout Specifies the number of seconds to wait.
Valid range is -1 to 99999 seconds.
/NOBREAK Ignore key presses and wait specified time.
/? Displays this help message.
NOTE: A timeout value of -1 means to wait indefinitely for a key press.
Examples:
TIMEOUT /?
TIMEOUT /T 10
TIMEOUT /T 300 /NOBREAK
TIMEOUT /T -1
Another method is to ping an invalid IP address for a certain amount of time:
PING 1.1.1.1 -n 1 -w 60000 >NUL
60000 = milliseconds
hi i love stockoverflow but sometimes the answers are too concise... so heres more than you asked for... the timer and many other snipits
#ECHO off
MODE CON:COLS=25 LINES=11
cls
color 0B
:taskkill
echo.
echo Program To Shutdown
echo.
set /p taskkill=
if "%taskkill%" == "%taskkill%" goto taskkillconfirm
exit
:taskkillconfirm
color 0B
:image
set image=/im
if "%image%" == "%image%" goto imageconfirm
exit
:imageconfirm
color 0B
:forced
set forced=/f
if "%forced%" == "%forced%" goto forcedconfirm
exit
:forcedconfirm
cls
:hour
color 0B
echo.
echo. Hours?
echo.
set /p hour=
:min
color 0B
cls
echo.
echo Minutes?
echo.
set /p min=
:sec
color 0B
cls
echo.
echo Seconds?
echo.
set /p sec=
:continue
color 0B
cls
echo %taskkill%
echo Program Shutdown in
echo %hour% Hours
echo %min% Minutes
echo %sec% Seconds
set /a sec="%sec%-1"
if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto done
ping -n 2 127.0.0.1 >NUL
goto continue
:done
color 0B
cls
taskkill %image% %taskkill% %forced%
exit
hope you really enjoy this answer
Simple, try this
#echo off
echo Do you want to read word file or pdf file? Hit any key for options...
pause >nul
echo w for word
echo p for pdf
::set input =
set /p input = Choose your option
if %input% == w goto w
if %input% == p goto p
:w
echo Reading Word file...
::start /your/path/to/your/Office/winword.exe
/path/to/your/doc/file/filename.doc
echo Timer starts
pause >nul
echo 10
ping localhost -n 2 >nul
cls
echo 9
ping localhost -n 2 >nul
cls
echo 8
ping localhost -n 2 >nul
cls
echo 7
ping localhost -n 2 >nul
cls
echo 6
ping localhost -n 2 >nul
cls
echo 5
ping localhost -n 2 >nul
cls
echo 4
ping localhost -n 2 >nul
cls
echo 3
ping localhost -n 2 >nul
cls
echo 2
ping localhost -n 2 >nul
cls
echo 1
ping localhost -n 2 >nul
cls
echo Time's up. Starting the next document...
::you can copy/paste the above commands to start another word file
:p
echo Reading Pdf file...
echo Timer starts
::start /your/path/to/your/Acrobat/acrord32.exe
/path/to/your/pdf/file/filename.pdf
pause >nul
echo 10
ping localhost -n 2 >nul
cls
echo 9
ping localhost -n 2 >nul
cls
echo 8
ping localhost -n 2 >nul
cls
echo 7
ping localhost -n 2 >nul
cls
echo 6
ping localhost -n 2 >nul
cls
echo 5
ping localhost -n 2 >nul
cls
echo 4
ping localhost -n 2 >nul
cls
echo 3
ping localhost -n 2 >nul
cls
echo 2
ping localhost -n 2 >nul
cls
echo 1
ping localhost -n 2 >nul
cls
echo Times up. Starting the next document...
::you can copy/paste the above commands to start another Pdf file
How do I create a batch file timer to execute / call another batch through out the day Maybe on given times to run but not to run on weekends ? Must run on system times can also be .cmd to run on xp server 2003
For the timer part of your script i highly reccomend using:
echo.
echo Waiting For One Hour...
TIMEOUT /T 3600 /NOBREAK
echo.
echo (Put some Other Processes Here)
echo.
pause >nul
This script waits for 1 hour (3600 seconds) and then continues on with the script and the user cannot press any buttons to bypass the timer (besides CTRL+C).
You can use
Timeout /t 3600 /nobreak >nul
If you don't want to see a countdown on the screen.
I would use the scheduler (control panel) rather than a cmd line or other application.
Control Panel -> Scheduled tasks
Below is a batch file that will wait for 1 minute, check the day, and then perform an action. It uses PING.EXE, but requires no files that aren't included with Windows.
#ECHO OFF
:LOOP
ECHO Waiting for 1 minute...
PING -n 60 127.0.0.1>nul
IF %DATE:~0,3%==Mon CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Tue CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Wed CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Thu CALL WootSomeOtherFile.cmd
IF %DATE:~0,3%==Fri CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Sat ECHO Saturday...nothing to do.
IF %DATE:~0,3%==Sun ECHO Sunday...nothing to do.
GOTO LOOP
It could be improved upon in many ways, but it might get you started.
The AT command would do that but that's what the Scheduled Tasks gui is for. Enter "help at" in a cmd window for details.
I did it by writing a little C# app that just wakes up to launch periodic tasks -- don't know if it is doable from a batch file without downloading extensions to support a sleep command. (For my purposes the Windows scheduler didn't work because the apps launched had no graphics context available.)
#echo off
:Start
title timer
color EC
echo Type in an amount of time (Seconds)
set /p time=
color CE
:loop
cls
ping localhost -n 2 >nul
set /a time=%time%-1
echo %time%
if %time% EQU 0 goto Timesup
goto loop
:Timesup
title Time Is Up!
ping localhost -n 2 >nul
ping localhost -n 2 >nul
cls
echo The Time is up!
pause
cls
echo Thank you for using this software.
pause
goto Web
goto Exit
:Web
rem type ur command here
:Exit
Exit
goto Exit
#echo off
:Start # seting a ponter
title timer #name the cmd window to "Timer"
echo Type in an amount of time (Seconds)
set /p A= #wating for input from user
set B=1
cls
:loop
ping localhost -n 2 >nul #pinging your self for 1 second
set /A A=A-B #sets the value A - 1
echo %A% # printing A
if %A% EQU 0 goto Timesup #if A = 0 go to ponter Timesup eles loop it
goto loop
:Timesup #ponter Timesup
cls #clear the screen
MSG * /v "time Is Up!" #makes a pop up saying "time Is Up!"
goto Exit #go to exit
:Exit
better code that doesn't involve ping:
SET COUNTER=0
:loop
SET /a COUNTER=%COUNTER%+1
XCOPY "Server\*" "c:\minecraft\backups\server_backup_%COUNTER%" /i /s
timeout /t 600 /nobreak >nul
goto loop
600 seconds is 10 minutes, however you can set it whatever time you'd like
You could also do this>
#echo off
:loop
set a=60
set /a a-1
if a GTR 1 (
echo %a% minutes remaining...
timeout /t 60 /nobreak >nul
goto a
) else if a LSS 1 goto finished
:finished
::code
::code
::code
pause>nul
Or something like that.