Executing 2 batch scripts consecutively - windows

I have 2 batch scripts:
sleep1.bat
echo sleep 4 seconds
timeout /t 4
exit
sleep2.bat
echo sleep 3 seconds
timeout /t 3
exit
Is it possible to execute sleep1.bat and sleep2.bat consecutively so that the program can sleep for a total of 7 seconds?
sleepMain.bat
call sleep1.bat
call sleep2.bat
pause
SleepMain.bat fails to do what I want. It only executes slee1.bat, because sleep1.bat contains exit.
sleep1.bat and sleep2.bat are written by others, I wish I don't have to change them.

Yes, exit will definitely exits, but it exit the console instead of the current batch script. To prevent that happen, use exit /b.
Tips:
Use exit only in the main script.
If you're just doing some simple thing in sleep1.bat, you can choose to not to add an exit /b command. Try it yourself : 1.bat - set "you=you", 2.bat - set "you=you" & exit /b
In 3.bat, try either call 1.bat & echo %you% or call 2.bat & echo %you%, you will still get you as the output. This mean either script with exit /b or none still can access the variables in the previous called batch script. (Safe to try with simple script)

As an altervative way to terminate execution of a batch script without exiting the current cmd instance, you do not need to put anything in a straight-forward script like yours (where one command is just run after the other, no execution branches, no if clauses, no loop structures). To tell the script explicitly to quit execution, you can also use goto :EOF. The :EOF is an implicit label that points to the end of the script (type goto /? in the command prompt for more details). This works only when command extensions are enabled, which is the case as per default (see cmd /? for that). To use this method without extension, just define the label :EOF by writing it as the very last line.
Your two scripts will not last 4 + 3 = 7 seconds, but something between 6 and 7 seconds actually. This is because the timeout command returns at certain second multiples, so the very first one may be shorter (0 s > actual delay time >= 1 s). The wait time can even be interrupted by keyboard interaction in your script; to prevent that, add the /NOBREAK option (see timeout /? for help). To really wait for 4 seconds for example, state > nul ping 127.0.0.1 -n 5; the given IP address means "home", so the host itself, therefore it is always valid; the 5 defines the number of ping attempts, so there are 4 one-second intervals in between; > nul redirects the display output to the nul device, meaning that nothing is displayed.

This works for me under Windows 10:
cmd /c sleep1.bat
cmd /c sleep2.bat
pause
It works by launching the batch scripts in a separate command instance. The output looks like this:
D:\workspace\deleteme>sleepMain.bat
D:\workspace\deleteme>cmd /c sleep1.bat
D:\workspace\deleteme>echo sleep 4 seconds
sleep 4 seconds
D:\workspace\deleteme>timeout /t 4
Waiting for 0 seconds, press a key to continue ...
D:\workspace\deleteme>exit
D:\workspace\deleteme>cmd /c sleep2.bat
D:\workspace\deleteme>echo sleep 3 seconds
sleep 3 seconds
D:\workspace\deleteme>timeout /t 3
Waiting for 0 seconds, press a key to continue ...
D:\workspace\deleteme>exit
D:\workspace\deleteme>pause
Press any key to continue . . .
D:\workspace\deleteme>

Related

How to execute a cmd file in a loop with a delay between each iteration?

I have a cmd file 'D:\ProgramFiles\test.cmd'.
I want to write a script which would execute this test.cmd in a loop n times, also, after each execution, I want a delay/timeout of 5 seconds.
How should I go about this?
The timeout command is not always available. For example when executing a batch file on a build server like Jenkins or when running on an older version of Windows. However, there is a way that will always work:
#ECHO OFF
FOR %%i IN (1,1,5) do (
CALL "D:\ProgramFiles\test.cmd"
ping 127.255.255.255 -n 1 -w 5000> nul
)
Here we are using the ping command. ping 127.255.255.255 -n 1 -w 5000> nul will ping the IP address 127.255.255.255. This address won't be reachable but -w 5000 will make the command "wait" for 5000ms = 5s for a response. The only limitation is that you can't go below 500 ms with this method. Any number below 500 will result in a delay of 500 ms.
You can use ping -n 6 localhost>nul as well. This will repeat the ping six times. Notice that you will need 6 repetitions to achieve a delay of 5 seconds as the first ping will be launched immediately so six pings mean 5 seconds delay. Don't fall victim to the famous "fencepost error"
For further information check this website.
a For loop is what you want. in this instance, we tell it to start counting by 1, steps of 1, end at 5, which means you will be running 5 loops. For more on the for run from cmd.exe for /?
Timeout, depends on your OS version. Older OS's does not have it, then perhaps look at the older version called sleep or alternatively use the command ping with a count of timeout you want, +1. timeout On some Windows versions you do not like the /t and therefore can be used without it. i.e timeout 5
#echo off
for %%i in (1,1,5) do (
echo We do something here..
timeout /t 5
)
To learn about Windows cmd/batch commands, and how to use them in a batch files you can open cmd and type help which will list loads of commands. each of them can then be run followed by the /? switch to get full help on each command.
Example (n = 200):
FOR /L %%A IN (1,1,200) DO (
D:/ProgramFiles/test.cmd
timeout /t 5
)

Batch Script - Wait 5 seconds before exec program [duplicate]

This question already has answers here:
How to wait in a batch script? [duplicate]
(6 answers)
How to sleep for five seconds in a batch file/cmd [duplicate]
(30 answers)
Closed 6 years ago.
I have the below script but it does not sleep before executing the software.
Any ideas?
#echo off
SLEEP 10
START "" "C:\Program Files (x86)\..."
There are (at least) the following options, as others already stated:
To use the timeout command:
rem // Allow a key-press to abort the wait; `/T` can be omitted:
timeout /T 5
timeout 5
rem // Do not allow a key-press to abort the wait:
timeout /T 5 /NOBREAK
rem // Suppress the message `Waiting for ? seconds, press a key to continue ...`:
timeout /T 5 /NOBREAK > nul
Caveats:
timeout actually counts second multiples, therefore the wait time is actually something from 4 to 5 seconds. This is particularly annoying and disturbing for short wait times.
You cannot use timeout in a block of code whose input is redirected, because it aborts immediately, throwing the error message ERROR: Input redirection is not supported, exiting the process immediately.. Hence timeout /T 5 < nul fails.
To use the ping command:
rem /* The IP address 127.0.0.1 (home) always exists;
rem the standard ping interval is 1 second; so you need to do
rem one more ping attempt than you want intervals to elapse: */
ping 127.0.0.1 -n 6 > nul
This is the only reliable way to guarantee the minimum wait time. Redirection is no problem.
Try the timeout command.
timeout /t 10
This shows Waiting for 10 seconds, press a key to continue ...
You can use the /nobreak switch that ignores user input (other than CTRL-C)
timeout /t 30 /nobreak
Or you can redirect its output to NUL for a blank screen that waits 10 seconds and ignores user input:
timeout /t 30 /nobreak > NUL

How to close CMD when another program is exited?

I want to close a cmd window when the program csgo.exe is exited. There is one cmd window open, which is ( node server.js ) and I would like the program to be stopped when csgo.exe is quit.
Here is the full .bat.
#ECHO OFF
start steam://rungameid/730
start cmd /K "cd C:\CSGO-HUD-master & node server.js"
start cmd /K "cd C:\Program Files (x86)\Mozilla Firefox & start firefox.exe http://localhost:2626/ & exit"
Thanks!
The is doable if you know the task name of the program you want to watch. Here is the code:
#echo off
:LOOP
tasklist|findstr calc.exe > nul
if %errorlevel%==1 goto ENDLOOP
ping 127.0.0.1 -n 2 > nul
goto LOOP
:ENDLOOP
exit
In this example we want our bat to terminate after the calculator (calc.exe) is closed.
To achieve this, we first execute tasklist which gives us a list of all running programs. Then we check the output for the occurrence of calc.exe. If it is still running, findstr calc.exe will set %errorlevel% to 0 so we can just jump back and check again. If calc.exe is not running, %errorlevel% will be set to 1 so we know that we can jump out of the loop and terminate.
I've added the line ping 127.0.0.1 -n 2 > nul to avoid busy waiting. This line will make your code "wait" for one second between two iterations. Consider that to adjust the waiting time you'll have to replace 2 by the desired amount of seconds to wait +1. So if you want to wait for five seconds the code would be ping 127.0.0.1 -n 6 > nul.

Making command prompt wait

#call JBOSSbuildWar.bat > ..\logs\JBOSSbuildWar_out 2> ..\logs\JBOSSbuildWar_err
start cmd /c #call WeblogicbuildWar.bat > ..\logs\WeblogicbuildWar_out 2> ..\logs\WeblogicbuildWar_err
start cmd /c #call FEBAPortletWebWar.bat > ..\logs\FEBAPortletWebWar_out 2> ..\logs\FEBAPortletWebWar_err
start cmd /c #call buildStaticJBoss.bat > ..\logs\JBOSSFEBAStaticWar_out 2> ..\logs\JBOSSFEBAStaticWar_err
I have this set of batch files getting executed in order. I want to fork out this so that they execute in parallel. I have done that using start cmd /c. Now this will fork out new command prompt window for each of them. Assume there are some set of statements after this. I want to make sure they get executed only after all the forked batch files are finished. How to achieve this?
Lets say average time taken by each file is:
JBOSSbuildWar- 30 minutes
WeblogicbuildWar- 35 minutes
FEBAPortletWebWar- 30 minutes
buildStaticJBoss- 35 minutes
Since the main command prompt window has completed its task in 30 minutes, and the forked batch files needs another 5 minutes to complete, I want the main cmd to wait until the others are done. Kindly help.
there's multiple commands, you can choose what one to use,
Pause
Will pause cmd until the user presses any key.
Displays message, "Press any key to continue..."
I use a certain variation of this command called pause>nul.
What it does is pauses the .bat, but doesn't show a message, You just get a blank screen!
TIMEOUT
Multiple syntaxes, very useful command, use this one quite a bit.
Sample code: TIMEOUT 10
Can be bypassed by a key press
Pauses the execution of command by 10 seconds.
You can also structure it like this: TIMEOUT 10 /nobreak
Which does not allow the user to press a key to skip it, they must endure it!
Ping
Very nice one.
Syntax is like this: PING 1.1.1.1 -n 1 -w 60000 >NUL
Probably most complex of the three.
PING can be used to delay execution for a number of seconds.
Hope I helped!
-Logan
Easy way:
In your main batch,
del "%temp%\*.processfinished" >nul 2>nul
...
start ....whatever1.bat...
start ....whatever2.bat...
start ....whatever3.bat...
...
wait4all:
for /L %%a in (1,1,3) do if not exist "%temp%\%%a.processfinished" timeout /t 1 >nul &goto wait4all
:: continues here when all (3) processes have finished.
del "%temp%\*.processfinished" >nul 2>nul
Then in each of the (3) subsidiary .bat files, create a file "%temp%\x.processfinished" where x=1 for the first process, 2 for the second and so on.
When the sub-processes have started, the procedure waits until each has created its own ".processcompleted" file by checking whether any of the 3 is missing, if it it, timeout for 1 second and look again. Only if the 3 files are present will it continue.
In all probability, it would be best if the subsidiary processes could take an extra parameter (the name of this sub-process's "processfinished" file) rather than having a fixed number for each.
You could extend this, and use say the date and time to augment the filename so that this entire process could itself be run many times in parallel.
BTW - by starting the procedure with
#echo off
you can remove all of the leading #s (all that does is suppress the command-reporting for that one line.)
Also, start is happier as start "" ....., that is, with an empty window title in quotes as its first argument. This allows other arguments to be "quoted" as necessary - the very first "quoted" argument used is used as the "window title" for the process and is likely to be lost to the sub-process. Routinely assigning a dummy (empty if necessary) "window title" means you don't trip over this problem in the future.
You can add this timer function before each of the commands:
#call JBOSSbuildWar.bat > ..\logs\JBOSSbuildWar_out 2> ..\logs\JBOSSbuildWar_err
Timeout /t 60 /nobreak >nul
start cmd /c #call WeblogicbuildWar.bat > ..\logs\WeblogicbuildWar_out 2> ..\logs\WeblogicbuildWar_err
Timeout /t 60 /nobreak >nul
start cmd /c #call FEBAPortletWebWar.bat > ..\logs\FEBAPortletWebWar_out 2> ..\logs\FEBAPortletWebWar_err
Timeout /t 60 /nobreak >nul
start cmd /c #call buildStaticJBoss.bat > ..\logs\JBOSSFEBAStaticWar_out 2> ..\logs\JBOSSFEBAStaticWar_err
Ps: 60 stands for 1 minute, if you want 30 minutes, change it to 1800

How to run a .vbs from a .bat

Created an extensive batch script program to handle some automated file management and printing and I need to call a vbs file for its sendkeys operation. Is there a way to accomplish this without freezing the program?
I've tried START /WAIT my.vbs and the script freezes when it enters the .vbs
Anyone have other methods or switches you would recommend?
I would like it to run silently if at all possible, and i need the /WAIT switch because I need the sendkeys operation to complete prior to the next step in the batch file.
Instead of using START /WAIT my.vbs you could try using cscript //NoLogo //B my.vbs. You can also pass other options to cscript that way.
Just Call The vbs file correct path
The BAT file Edit it...!!!
wscript "file-path"
Example:
wscript "D:\KmaniZoro\PGM\N++\VBS\inputbox.vbs"
timeout 5
timeout /?
TIMEOUT [/T] timeout [/NOBREAK]
Description:
This utility accepts a timeout parameter to wait for the specified
time period (in seconds) or until any key is pressed. It also
accepts a parameter to ignore the key press.
Parameter List:
/T timeout Specifies the number of seconds to wait.
Valid range is -1 to 99999 seconds.
/NOBREAK Ignore key presses and wait specified time.
/? Displays this help message.
NOTE: A timeout value of -1 means to wait indefinitely for a key press.
Examples:
TIMEOUT /?
TIMEOUT /T 10
TIMEOUT /T 300 /NOBREAK
TIMEOUT /T -1
Create the .vbs file Then open the Batch file and enter START "" "FILE PATH"
EG: Start "" "C:\Users\%Username%\Desktop\Spiritual Aid\Program\2.vbs"
IT WORKED PERFERCTLY IN MY COMPUTER.

Resources