Server re-starter Timeline, Seconds Minutes Hours - windows

How can I make my command/ batch file show a better timeline?
#echo off
title Server Restarter
:start
set time=18000
start (exefile).exe
:loop
cls
IF %time% GTR 0 (
set /a time=%time% - 1
set /a min=%time%/5
set /a hrs=%time%/10
echo (servername) will Restart In %time% Seconds.
echo (servername) will Restart in %time% Seconds, %min% Minutes, %hrs% Hours.
ping 127.0.0.1 -n 2 > NUL
goto loop
)
taskkill /f /im (exefile).exe
cls
close server
goto start
Is there a way to make the code show like this,
Minutes 0-59 Seconds 0-59 Hours 0-24 Days 0-(number)
etc? I tried many thing but couldn't get the hours to show correctly.

Your calculation is a bit too easy.
#echo off
set tim=180000
:loop
cls
IF %tim% leq 0 goto done
set /a day=%tim%/86400
set /a hrs=(%tim%-day*86400)/3600
set /a min=(%tim%-day*86400-%hrs%*3600)/60
set /a sec=%tim%-day*86400-%hrs%*3600-%min%*60
echo servername will Restart In %tim% Seconds.
echo servername will Restart in %day% days, %hrs% hours, %min% Minutes and %sec% seconds.
ping 127.0.0.1 -n 2 > NUL
set /a tim-=1
goto loop
:done
echo done.
Your main problem was that your code uses variable in a block (inside (and )), so that you would need delayed expansion. You can avoid it by clever use of the condition.
By the way: i used %tim%instead of %time% because %time% is a system variable - bad idea to mess with them - even if it works.

Related

How to use the timeout command in batch language?

I actually wanted to create a timer using batch program and when I run the code the output is not the way I want. The screen continues flicking without decreasing the second. Can someone Help me. Thanks alot.
#echo off
set countdown=10
:loop
if %countdown% == 0 goto end
cls
echo %countdown%
timeout /t 1 /NOBREAK
set/a count=%countdown%-1
goto loop
:end
cls
echo Time's up!
pause
You should replace this line set/a count=%countdown%-1 by this one set /a countdown=countdown-1 or by a shortened set /a countdown-=1.
#echo off
set "countdown=10"
:loop
if "%countdown%" EQU "0" goto end
cls
echo %countdown%
set /a countdown-=1
timeout /t 1 /NOBREAK>nul
goto loop
:end
cls
echo Time's up!
pause
A way to do it without the goto / label
#echo off
Set count=10
Setlocal enableDelayedExpansion
For /L %%a in (1,1,!count!) DO (
Set /a count=!count! - 1
(ECHO <esc>[1;1H<esc>[K!count!)
(
TIMEOUT 1 >nul
)
)
ECHO times up!
pause
<esc>[1;1H<esc>[K Sets the position of the text (line;column) and clears the line from that point. <esc> represents the ANSI escape character.
Using the ANSI cursor positioning allows you to refresh text on screen selectively, without having to clear the whole screen.

Batch file IF GEQ triggers despite not matching

I'm trying to start and .exe at a certain time frame, but despite not matching GEQ yet (e.g. 20:55), it still goes to the :run label.
I've also tried only EQU, but here, despite matching the time, it doesn't goto :run. Only if i start the batch at the exact %time%, it works, but obviously that's missing the whole point.
Whats wrong here?
#ECHO OFF
SET hour=%time:~0,5%
echo It is %hour%
:check
echo.
echo %time:~0,5%
echo checking
timeout /t 60
IF %hour% GEQ 21:00 IF %hour% LEQ 22:00 (goto :run) else (goto :check)
:run
echo running
start chrome.exe
pause
May be you can try something like this:
#echo off
:get_the_time
for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do #for /f %%# in ("%%#") do #set %%#
echo CURRENT TIME -- %hour%:%minute%
:: creatring a comparable number with wich time for starting chrome can be used
if %hour% LSS 10 ( set hour=10%hour% ) else ( set hour=1%hour%)
if %minute% LSS 10 (set minute=0%minute%)
set comparable_time=%hour%%minute%
::now the comparable_time is in format 1HourMinute . The 1 in the front is to avoid complications with leading zero
timeout /t 60
if %comparable_time% GEQ 12100 if %comparable_time% LEQ 12200 ( goto :run ) else ( goto :get_the_time )
:run
echo running
start chrome.exe
pause
As your method of getting time is not so robust and depends on the time settings in the control panel I preferred using the WMIC.
When there are non numerical symbols in the things you want to compare with IF it will make an alphabetical comparison so the : is not part of the IF clauses now.
Also a comparison is made with variable looking like 1Hourminute and the hour and minutes are kept in two digits format so now only a comparisons with numbers without leading zeros are used.
It is easy enough to handle dates in PowerShell. Create two files in the same directory.
=== runitat.ps1
$h = (Get-Date).Hour
if (($h -ge 20) -and ($h -le 21)) {
Invoke-Command -Command {& "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"}
}
=== runitat.bat
:head
powershell -NoLogo -NoProfile -File %~dp0runitat.ps1
timeout /T 60
GOTO head

Implementing timed input in batch file. (countdown to a minute)

I am using windows 8 to write a batch file and I got stuck in implementing the timer in batch file. I want to ask input from the user and give them one minute to type their input. Once the time hit a minute then display message like 'the time is over'. So, the time will start from 1 second and ends at 60 seconds OR start from 60 seconds and going down to 0 second. Either works just fine.
Additionally, I want timer to be displayed somewhere on screen so that they can see the countdown. Also, while the timer is running I want the user to be able to type a word and hit enter. This program will not make user wait, but it will wait until the time is over OR as soon as the user enter a word (whichever comes first). After they enter a valid word then I want to store that word in certain variable and do something like (goto VALIDWORD OR echo That is a valid word!)
I don't know if this is possible in batch file and there are more advanced language to use, but I want to complete this program using batch scripting. Thank you.
Following is my concept:
#echo off
:Start
color EC
set /a time =60
:loop
cls
if %time% EQU 0 goto Timesup
if %time% LEQ 60 goto CONTINUE
:CONTINUE
ping localhost -n 2 >nul
set /a time-=1
set /p cho= Enter your word:
echo Remaing Time: %time%
goto loop
:Timesup
echo The time is over!
exit
Any ideas or support would be appreciated. Again Thanks!
Batch files are not designed for tasks like this one, but it is possible to perform certain advanced managements although in a limited manner. The subroutine below use choice command to get input keys, so it does not allow to end the input with Enter key nor to delete characters with BackSpace; it use 0 and 1 keys for such tasks.
#echo off
setlocal
call :ReadTimedInput 60 word=
echo Word read: "%word%"
goto :EOF
:ReadTimedInput seconds result=
setlocal EnableDelayedExpansion
set /A seconds=100+%1
set "letters=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
echo Enter a word; press 0 to End, press 1 to Clear
:clear
set /P "=X!CR! !CR!" < NUL
set "result="
:loop
set /P "=X!BS! !CR!%seconds:~-2%: %result%" < NUL
choice /C ¡10%letters% /N /CS /T 1 /D ¡ > NUL
if %errorlevel% equ 1 goto tick
if %errorlevel% equ 2 goto clear
if %errorlevel% equ 3 echo/& goto endInput
set /A char=%errorlevel%-4
set "result=%result%!letters:~%char%,1!"
:tick
set /A seconds-=1
if %seconds% gtr 100 goto loop
echo !CR!00
set "result=Time out"
:endInput
endlocal & set "%2=%result%"
exit /B

Delay in a 'for' loop in a batch file

I have this code, and I want to make a delay in the for loop to measure one time and then make a delay and continue after that.
I tried:
timout / t 10/ nobreak
As it is shown in the code, but it didn't work.
: #echo off
set Looping_number=10 or anything else
FOR /L %%A IN (1,1,%Looping_number%) DO call :doit %%A
goto :eof
:doit
set pad=00%1
set num=%pad:~-2%
#set var1=var1.exe
#set var2=C:\...\...\... .txt
#set output=C:\....\output\%num%
Mkdir %output%
%var1% %var2% %Results%
timeout / t 10 / nobreak
goto :eof
But it says you can not delay?
How can I fix this problem?
You have extra spaces in your command:
timeout / t 10 / nobreak
You need to say:
timeout /t 10 /nobreak
You can try the ping method:
ping -n 10 localhost > nul

Batch: Trying to get a command to execute at a specific amount of seconds

This works for hours, running in a loop as soon as it hits 13 hours it executes.
#ECHO OFF
:time
echo %time%
FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~0,2%") DO SET /a HH=%%b
IF %HH% equ 13 goto success
goto time
:success
echo success finally
pause
goto time
but if I run this for seconds in a loop for it to execute at 13 seconds it just keeps running in a loop and doesn't execute the command.
#ECHO OFF
:time
echo %time%
FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~5,2%") DO SET /a SS=%%b
IF %SS% equ 13 goto success
goto time
:success
echo success finally
pause
goto time
This Doesn't Work either
FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~6,2%") DO SET /a SS=%%b
I need this to execute at any specified amount of seconds. So that instead of for instance executing at 13:00:00.00. I can get it to execute at 13:00:13.00 or something like that.
Needs to be a 6 not a 5 for the offset.
%time:~6,2%
It does not have to be complex. This works, but unless you provide more details as to what you are trying to accomplish we cannot provide the best help.
:time
echo %time%
IF %time:~6,2% equ 13 goto success
goto time
Note that using the time environment variable is user locale specific.
Use something like for no locale format time.
wmic path win32_operatingsystem get LocalDateTime

Resources