sleep function in a batch script - batch-file

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

Related

Can I slow down the speed of a 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.

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

Delay Code to start the exe file

I have this bat file to just kill and start the file, but I need a delay.
TASKKILL /F /IM "file.exe"
(what is the code to delay 1 minute before executing the code below?)
start /d "path" file.exe
Can you please help me? Thanks
You can use ping as #Krister suggested
ping 127.0.0.1 -n 60
of if you are using Vista and above you can use the much easier timeout
timeout /t 60
I have used the ping command to achive this, you could ping an invalid host and set the timeout for the command to your desired delay.
#echo off
ping 1.2.3.4 -n 1 -w 60000 >NUL
# this command will be run after 60000ms
echo 'running';
pause

Batch file - Display text for 5 seconds

How can I display my data from x.bat for 5 seconds?
When this code runs, it's impossible for me to see anything, it opens and closes immediately.
#ECHO OFF
:BEGIN
ECHO Authorized user
:END
If I use pause, the user still needs to hit a key to close the screen, that's why this should happen automatically.
#ECHO OFF
:BEGIN
ECHO Authorized user
pause
:END
Thanks
#ECHO OFF
:BEGIN
ECHO Authorized user
timeout 5 >nul
cls
:END
You can grab "sleep.exe" from the Windows Server 2003 resource kit and use sleep [seconds] but the easiest way to get a set delay is to simply ping localhost N+1 times, where N is the number of seconds.
This will sleep for five seconds (there's no delay before the first ping, and a 1s delay after each):
ping -n 6 localhost>nul
SLEEP 5
GOTO:EOF
Would wait for 5 seconds before closing the window.
On Windows Vista and later you can use timeout:
timeout 5 >nul
On ancient Windows versions you need to resort to ping:
ping -n 6 localhost >nul 2>&1
An alternative way to use ping, which also seems to be slightly less precise:
ping -n 1 -w 5000 1.0.0.0 >null
That is, convert seconds to milliseconds and use that as an argument of -w parameter. The -n parameter should have the argument of 1. The host should be a knowingly inaccessible IP address.
variety of ways to resolve this
timeout /t 5 /nobreak >Nul
::for 5 sec
#PING 1.1.1.1 -n 2 -w 3000>nul
:: -n 2 for around 5-6 seconds
sleep 5 (EMERGENCY CASE)

Resources