Delay in a 'for' loop in a batch file - windows

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

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 progress percentage

How can I show the progress of a long running operation in windows batch (cmd) file in percentage? Can you share some example code?
Here is how...
Note: This code is a slightly modified version of this answer.
#echo off
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
FOR /L %%n in (1,1,10) DO (
call :show_progress %%n 10
ping localhost -n 2 > nul
)
echo Done!
exit /b
:show_progress
setlocal EnableDelayedExpansion
set current_step=%1
set total_steps=%2
set /a "progress=(current_step * 100) / total_steps"
set /p ".=Progress: !progress!%%!CR!" <nul
if !progress! equ 100 echo.
exit /b
An example would be scanning a large file/database, showing progress instead of a blinking cursor, for ages!
set c=<no. of lines in file>
for /l %i in (1,1,%c%) do cls & set /p="Scanning for target ... "<nul & (set /a per=%i00/%c% >nul & echo !per!%)& ping 127.0.0.1 -n 2 > nul & REM when target found, send agents to greet
echo Complete.
The code in brackets () are for progress percentage.
Tested in Win 10 CmD>

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

Server re-starter Timeline, Seconds Minutes Hours

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.

Using Sleep with findstr in a .bat

I created a .bat file with the below lines
cd C:\MyFolder
d:
findstr "Apple" C:\log.txt |findstr "red" > red_apples.txt
SLEEP 3600
GOTO START
When the bat is executed, the SLEEP is not working and the commands are running continously.
Is there anything wrong with the code? Please help !
I don't believe Windows has a sleep, you can emulate it with ping, as shown in this example chkwait.cmd script:
#setlocal enableextensions enabledelayedexpansion
#echo off
echo %time%
call :waitfor 20
echo %time%
endlocal
goto :eof
:waitfor
setlocal
set /a "t = %1 + 1"
>nul ping 127.0.0.1 -n %t%
endlocal
goto :eof
The call :waitfor 20 in the above script will wait for twenty seconds:
pax> chkwait
10:18:13.42
10:18:33.51
SLEEP does not exist in windows batch script. You would have create your own Sleep wrapper EXE and call that from the batch. Or use the clever trick from #paxdiablo above.

Resources