Delay Code to start the exe file - batch-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

Related

Make a batch-file loop when there is internet or launch a program and exit when there is NO internet

The idea is to have a batch file ping the internet every 5 minutes to see if there is a connection. If there is an internet connection the batch file will loop every 5 minutes to ping again. If there is NO internet connection then the batch file will launch a program and exit. I created a batch file that does everything but exit when the internet is not detected.
What I have so far loops just the way it should but I'm stuck at making the file exit if the internet connection is NOT connected and notepad.exe is launched. I don't know much about batch-files and am trying to piece things together from searches, I need help.
#echo off
setlocal
cls
:loop
#ping 209.222.18.218 -n 1 -w 1000> nul
if %ERRORLEVEL% EQU 1 start C:\windows\notepad.exe
timeout /t 60 >null
goto loop
Expected result: the batch file pings the internet every 5 minutes to detected either there is or isn't an internet connection. If there is an internet connection the batch file will loop every 5 minutes. If there isn't an internet connection the batch file will launch notepad.exe and then exit itself.
Actual result, I get the batch file to loop when it detects internet but I can't get it to exit itself when there is NO internet.
Rearrange your lines a bit:
:loop
timeout 300 >nul
ping 209.222.18.218 -n 1 -w 1000 |find "TTL=" >nul
if %errorlevel%==0 goto :loop
REM if not errorlevel 1 goto :loop
start C:\windows\notepad.exe
Use one of the if lines - whichever you are more comfortable with.

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

My Batch file wont continue while windows is locked

I have a batch file which calls other programs to open and run.
I want this batch file to run overnight while my computer is locked. I use Windows Task Scheduler to run this program overnight. The batch file runs fine except when it gets to start Check1.msl. Then windows can't seem to find that file. When I unlock my computer and manually execute the batch file, it has no problem finding and running those files.
Here is my code:
#echo off
Start Check1.msl
PING 1.1.1.1 -w 1000 -n 1
start passwordinjector.vbs
IF EXIST C:\Users\Username\Desktop\New folder\check.rpt (
taskkill /IM MAINRDW.exe >nul
PING 1.1.1.1 -w 500 -n 1
Start Trial.msl
PING 1.1.1.1 -w 1000 -n 1
start passwordinjector.vbs
Del C:\Users\Username\Desktop\New folder\check.rpt
) ELSE (
PING 1.1.1.1 -w 500 -n 1
taskkill /IM MAINRDW.exe >nul
)
EDIT:
I've been trying it out for awhile now. To test it, I simply use Windows Task Scheduler to run it a minute out then lock my computer and wait for the Scheduler to kick in.
Here is what I have so far:
#echo off
cd /d "%~dp0"
PING 1.1.1.1 -w 2000 -n 1
echo starting the check1 msl file
Start Check1.msl
PING 1.1.1.1 -w 2000 -n 1
echo starting the password injector file
WScript //B passwordinjector.vbs
IF EXIST C:\Users\USERNAME\Desktop\New folder\check.rpt (
echo check if the check exists
taskkill /IM MAINRDW.exe /f >nul 2>&1
PING 1.1.1.1 -w 2000 -n 1
echo It does exist, so run the next Trial msl
Start Trial.msl
PING 1.1.1.1 -w 3000 -n 1
WScript //B passwordinjector.vbs
Del C:\Users\USERNAME\Desktop\New folder\check.rpt
) ELSE (
PING 1.1.1.1 -w 1000 -n 1
it doesnt exits
taskkill /IM MAINRDW.exe /f >nul 2>&1
)
exit
It is very hard to troubleshoot batch files. If anyone has any suggestions, I would appreciate it. Through trial and error, I figured out (still not sure) that it wasn't executing the .vbs script. So I looked it up online from Microsoft's developer network and got the syntax WScript //B.
EDIT2:
I put a pause into the code to break it down and make sure it is doing what it is supposed to do. Its not.
The Check1.msl will spit out a file named check.rpt. When i run it manually and the batch file pauses - check.rpt is there. I schedule the task and lock my computer. When I log back in, the msl program is open and the cmd prompt is sitting at pause but there is NO check.rpt file.
Here is what I have at the beginning:
#ECHO OFF
PUSHD "%~dp0
ECHO CD is now %CD%
PING 1.1.1.1 -w 2000 -n 1
echo starting the check1 msl file
DIR Check1.msl
Start Check1.msl
PING 1.1.1.1 -w 2000 -n 1
echo starting the password injector file
WScript //B passwordinjector.vbs
pause
I've tried cscript passwordinjector.vbs to no avail.
The .bat script can make the current working directory to be the one in which it is located by:
PUSHD "%~dp0"
Remember to POPD before exiting.
#ECHO OFF
PUSHD "%~dp0"
ECHO CD is now %CD%
DIR Check1.msl
START Check1.msl
...
It seems like the issue lies in the VBScript. I use .sendkey to inject the password. This is retricted while the computer is locked; so I'm out of luck.

Start a file from batch file wait specific time then close

I am trying to start a program through a batch file wait for some time and then close it. The code i wrote is this
#echo off
xfoil.exe < airfoil.txt > xfoil.out
ping 127.0.0.1 -n 1 -w 10000
taskkill /IM xfoil.exe /F
unfortunately it doesn't work..! I am using Windows 7
Can you please help me out..?
Thank you
Your ping line has a couple of problems. Firstly, the first ping result is always instant, so you should add 1 to the -n switch. Next, 127.0.0.1 is a valid address, so the wait timeout is useless.
Try this instead.
ping -n 11 0.0.0.0 >NUL
That should give you a 10-second pause.

Batch-file to run executable on time interval

I'm trying to create a .bat file to execute a flash video on a 10 s time interval in an infinite loop. So far I have...
ECHO OFF
:TOP
START /d "path" program.exe
SLEEP 10
TASKKILL /F /IM program.exe
GOTO TOP
It needs to restart the video after 10 seconds, but it just turns it on and that's it. Help!
Try this instead:
#echo off
:TOP
PathDir\program.exe
ping 127.0.0.1 -n 11
taskkill /im program.exe /f
goto :TOP
Haven't changed much but I haven't used SLEEP so not sure if that's causing the issue.
#echo off
#color 0a
:top
echo sleeping... sleeping...
sleep 5
goto top
echo waking... yargh!!! how dare yee wake thy neighbroseph?!
Also be sure to have this .bat file residing in the same directory as Sleep.exe which can be found: http://www.computerhope.com/dutil.htm
Tested and working exactly how I'd hoped.

Resources