Can I slow down the speed of a batch file? - batch-file

I am trying to write an ASCII movie in batch, and I need the script to echo a new line at about 10-15 lines per second (hopefully that's not too confusing). By default, the script goes insanely fast, making the whole thing more difficult to see.
What I would like to do is be able to set the execution speed of the batch file, and NOT the computer itself.
Here's an example of what I'd like:
#echo off
REM Below command sets script execution speed in lines per second
setspeed=10lps
REM Ten lines of 03 begin
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
echo The time is 13:20:03
REM Ten lines of 03 end
REM Ten lines of 04 begin
echo The time is 13:20:04
echo The time is 13:20:04
.....
Thanks!

Update: I found out that if you download NirCmd (link is at the bottom of the page), it includes a "wait" command that works quite well. In my batch script, I did this:
#echo off
::This tells NirCmd to wait for 5 seconds, and then proceed to the next command.
nircmd wait 5000
echo This is one line
nircmd wait 5000
echo This is another line
nircmd wait 5000
echo Oh, hey! Another line!
nircmd wait 5000
echo Take a guess what this is... Another line!
...

I like using choice commands. For instance:
#echo off
echo this is one line
choice /c q /d q /t 1 >nul
echo this is the second line
You can change the number after /t to set the seconds to wait. You can also press Q to skip the wait at any time.

In newer Windows OSes, there is the TIMEOUT command as well:
TIMEOUT /T 5
waits 5 seconds and displays a countdown like Waiting <countingDown> seconds. Proceed with any key.
TIMEOUT /T 5 > NUL
does the same without displaying anything.
TIMEOUT /T 5 /NOBREAK > NUL
additionally removes the possibility to speed up by pressing a key.
Bad thing: Only complete seconds possible, no millis.

TIMEOUT /T x > NUL
(replace the X with the number of seconds you want)
This will do a hidden countdown because of the > NUL command at the end you won't see the countdown/pause/sleep/timer and then it will proceed with the next command. This is the best to use, as I found the Timeout command with the NUL at the end to be the best many of the other commands like Sleep and Pause do not work.
You can also download NODEJS to get more functions or switch to powershell.
TIMEOUT /T 5 > NUL
Above command will give 5 second timed countdown with NO display, pressing any key will not stop it unless you use CTRL+C.

Related

Loop batch file for X minutes?

I am very new to batch files, having mostly coded in Matlab for years. I would simply like to loop some code I have written for a set number of minutes. In pseudocode:
While elapsedTime<timeLimit
Run Code
How do I declare the variables elapsedTime (which would be a time counter that runs throughout the While loop) and timeLimit (just a value in seconds)?
If you are using batch you could do this:
#echo off
set /p min=Set minutes:
set /a sec=%min% * 60
timeout %sec%
Calculating with time is difficult in batch (not impossible, just difficult).
Here is another approach: creating a dummy file, start an additional (independent) process which waits a certain time and then deletes the file.
The original process can loop until the file disappears:
#echo off
break> timer.tmp
start /min cmd /c "timeout 5 & del timer.tmp"
:loop
echo waiting...
timeout 1 >nul
if exist timer.tmp goto :loop
echo done waiting.
Just adapt the timeout to the desired time (5 seconds in my example)

sleep function in a batch script

I am creating a batch file to run a program on my desktop xyz.exe for 4 hours, then close it for 1 hour and repeat the process. Here is my script.
:a
START C:\Users\Mukul\Desktop\xyz.exe
SLEEP 14400
taskkill /F /IM xyz.exe
SLEEP 3600
goto :a
According to here, the script should wait. It also says:
SLEEP 10
will delay execution of the next command by 10 seconds. so SLEEP 14400 should delay the execution by 4 hours.
Current results:
Next command gets executed as soon as the first command completed.
Desired results:
Next command should wait for 4 hours before executing the last command.
You can use bash's timeout command.
For example:
#echo off
echo Hi
timeout /t 1 /nobreak > nul
/t is not mandatory
1 is the amount of second(s) to wait
/nobreak ensures the user can't skip the wait
> nul redirects output to nothing, so you don't see anything
SLEEP command may not be supported by your Windows version. Try this:
:a
START C:\Users\Mukul\Desktop\xyz.exe
TIMEOUT 14400
taskkill /F /IM xyz.exe
TIMEOUT 3600
goto :a
First off: Bash and Batch are very different languages.
Now, the answer.
I prefer the ping command over the sleep command, for it's easy switching between seconds and milliseconds:
ping -n 11 127.0.0.1>nul
That command pauses for 10 seconds then resumes.
Or:
ping 1.1.1.1 -n 1 -w 10001 >nul
Which also pauses for 10 seconds, but in milliseconds.
Either can be adapted into:
:a
start C:\Users\Mukul\Desktop\xyz.exe
ping -n 14401 127.0.0.1>nul
taskkill /F /IM xyz.exe
ping -n 3601 127.0.0.1>nul
goto a
DISCLAIMER: I have NOT tried the final piece of code (because I don't have 4 extra hours to do something).

Need batch for Netstat -anbo to run ever 30 minutes

Title description as stated. I'm very novice at this, so please go easy. I've searched articles here, and have tried several iterations of how to do this but my attempts have failed.
I need to run Netstat -anbo every 30 minutes to a text file and keep them rolling for at least 5 files.
Thanks.
Here's how you can run something every few secs in batch script, without the 'sleep' utility.. I will leave the implementation up to you :)
# :loop
REM "do something every 10 secs"
# ping localhost -n 11 > nul
# goto loop
The ping acts as a sleep here.. you can even ping to any non-existent host.
If you want a more accurate time interval, you are better off using
# :loop
REM Execute the MS-DOS dir command ever 20 seconds.
# SLEEP 20
# GOTO loop
For this to work, you will need to have a sleep MS-DOS utility on the computer. You can find it here
You would have to use a simple loop and timeout
A sample implementation
#echo off
:loop
netstat -anbo >> file.txt # >> Will append to file instead of erasing file content
timeout /t 1800 /nobreak # /t timeout in seconds /nobreak Ignore key presses and wait for specified time
goto :loop #Simple loop to keep batch running indefinitely
Remember to run this batch as administrator (For netstat)

Batch file - restart program after every 20 minutes

I want to create batch file which starts a program and after 20 minutes will close the program and start it again.
The only thing I know about a batch file is how to start a program:
#echo off
Start [adress of application]
This works:
#echo off //Turn off screen text messages
:loop //Set marker called loop, to return to
start "Wicked_Article_Creator" "C:\Wicked Article Creator\Wicked Article Creator.exe" //Start program, with title and program path
timeout /t 1200 >null //Wait 20 minutes
taskkill /f /im "Image Name" >nul //Image name, e.g. WickedArticleCreator.exe, can be found via Task Manager > Processes Tab > Image Name column (look for your program)
timeout /t 7 >null //Wait 7 seconds to give your prgram time to close fully - (optional)
goto loop //Return to loop marker
#echo off
:loop
start yourtarget.exe ...
timeout /t 1200 >null
taskkill /f /im yourtarget.exe >nul
goto loop
should do the job.
For anyone coming across this old question:
Instead of timeout.exe you can also ping to an address that definitely doesn't exist:
ping 999.199.991.91 -n 1 -w 60000 >NUL
You can change 60000 to whatever delay you want (in ms).
1 second = 1000
10 seconds = 10000
60 seconds = 60000
And so on..
#echo off
:loop
timeout /t 20 >null ( Wait for 20 seconds before kill program)
taskkill /F /IM terminal.exe
timeout /t 3600 >null ( wait for 1hr before returning to loop or recycle the process)
goto loop
I use another program called mt4bar to monitor my application and relaunch them anytime they crash or get closed

Run DOS Command for a Time Limit

I want to perform the following operations.
Read 1 word at a time from an input file consisting of many words.
Pass this word as an argument to another command line based application.
Run that application for a fixed amount of time, say 10 seconds.
Abort the execution of the application if it is still running after 10 seconds and go back, pick the next word from the input file and repeat steps 1 to 3.
Here is what I have written though it does not achieve exactly what I want it to:
#echo off
for /f %%i in ('type input.txt') do call:Routine %%i
:Routine
set app="myApp.exe"
set limit=60
%app% %1
goto Delay
:Delay
ping localhost -n %limit% > nul
The above script will introduce the delay after the execution of myApp has completed. However, I want it to run myApp.exe for not more than 10 seconds, if it does, then abort the application using taskkill and move on to the next word from the input file.
I searched for a solution online and came across this:
http://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.general/2006-04/msg03609.html
Though it does not answer my query exactly, I would like to make my code do something similar.
Thanks.
The logic in the linked code looks flawed: It either launches 3 download commands, or it delays ~59 seconds and attempts to kill all download commands, but it never does both. The TASKKILL command arguments are not correct - the imagename belongs after the /IM parameter.
In your code, you are not going to kill your task without the TASKKILL command!
You must GOTO :EOF or EXIT /B after your loop finishes, otherwise the code will fall through and execute the subroutine without using CALL. But there really is no need to use a subroutine at all.
You only need to initialize your variables once.
No need to execute a command in your IN() clause. FOR /F has a variation that can read the text file directly. Type HELP FOR from the command line and read the documentation carefully.
PING has roughly a 1 second delay between each echo request. So a count of 11 will yield a delay of roughly 10 seconds.
EDIT - originally forgot the critical START command to start the app in its own process
#echo off
set app="myApp.exe"
set limit=11
for /f %%i in (input.txt) do (
start "" %app% %%i
ping localhost -n %limit% > nul
taskkill /im %app% /f
)

Resources