I'm trying to run some console application .exe files from a batch file in Windows.
However, when I run the following code it only starts the first of the apps:
"C:\Development\App\bin\Debug1\Application.exe"
timeout 5
"C:\Development\App\bin\Debug2\Application.exe"
timeout 5
"C:\Development\App\bin\Debug3\Application.exe"
timeout 5
"C:\Development\App\bin\Debug4\Application.exe"
timeout 5
"C:\Development\App\bin\Debug5\Application.exe"
timeout 5
(I've included the timeout to spread out the intial processing a bit)
Is there a way to get the script file to start the first application, then move on and start the others?
Ideally I would like the script file to start all applications in a subdirectory, so that if I had Debug\Applications\*.exe or similar it would start all applications of type .exe (and possibly waiting 5 seconds between each). Is this possible?
You can start applications in the background by using start:
start "C:\Development\App\bin\Debug1\Application.exe"
Use start /? from a command window to get further details.
For example,
start dir
will open a new command window and show you a directory listing, leaving it open when finsished.
The:
start cmd /c "ping 127.0.0.1 && exit"
command will open a new window, run a four-cycle ping on localhost then exit.
In both cases, the current window will await the next command immediately.
#echo off
for %%F in ("Debug\Applications\*.exe") do (
start "" "%%F"
timeout 5
)
Related
I'm trying to build a .bat file that will run an executable Java SpringBoot web app jar file (keeping the cmd window open so that I can verify it started cleanly and close it/kill the process when I'm done), then wait 10 seconds to give the app time to start, then finally open it's URL in my web browser.
I've been able to get my intended functionality by breaking it down into two .bat files. The code I have below does what I want (except the echo message is repeated, but that's not a big deal).
I'd like to know how I can achieve the same functionality within a single .bat file.
I have launch.bat:
start wait.bat
java -jar C:\dev_tools\myapp.jar
which calls wait.bat:
echo Waiting for app to start before launching browser...
timeout 10
start http://localhost:8013/myapp/ && exit
Given the combined script is called launch.bat, put if not "%~1" == "" goto :JUMP on top, then the contents of launch.bat but with the first line changed to start launch.bat #, then place goto :EOF, then :JUMP, then the contents of wait.bat:
if not "%~1" == "" goto :JUMP
start launch.bat #
java -jar C:\dev_tools\myapp.jar
goto :EOF
:JUMP
echo Waiting for app to start before launching browser...
timeout 10
start http://localhost:8013/myapp/ && exit
When you now start launch.bat, it first checks if there is an argument, which should not be the case initially; so the first start command line is reached where the script executes itself, but with an argument (#) this time; the initially executed instance continues executing the rest until goto :EOF is reached, which terminates execution.
The recursively called instance will immediately continue execution at label :JUMP, where the code of the original wait.bat script is placed.
I think, this should work:
#echo off
start "MyApp" java.exe -jar C:\dev_tools\myapp.jar
echo Waiting for app to start before launching browser...
%SystemRoot%\System32\timeout.exe 10
start http://localhost:8013/myapp/
java.exe is started as separate process running parallel to cmd.exe instance processing this batch file.
So immediately after starting java.exe the information line is output by cmd.exe in initially opened console window.
Then timeout is executed to wait 10 seconds before finally the application is started with the HTTP URL.
Finally cmd.exe finishes processing the batch file as reading end of batch file which results in terminating the cmd.exe if started with option /C as done on double clicking on a batch file.
The Java application started as separate process keeps running independent on termination of cmd.exe processing the batch file.
I hope this is what you want.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
start /?
timeout /?
bat files from another Main.bat file
Files contain something like follows and i want both to launch in 15 seconds delayed and stay untill i close each one of them with a "Ctrl+C", can someone please help me with this Use Case please.
Main.bat
echo Task-1:
call C:\Users\user\bat\My_bat1.bat
echo Task-2:
call C:\Users\user\bat\My_bat2.bat
My_bat1.bat
start /wait cmd.exe /k "cd PATH && mvn -P dev"
My_bat2.bat
start /wait cmd.exe /k "cd PATH && mvn -Dspring.profiles.active=dev,swagger,no-liquibase -Dspring.cloud.config.profile=dev -DskipTests=true"
If you are just asking how to delay your batch files by 15 seconds you can do it like this:
choice /t 15 /D y /n
If not can you please clarify your question?
EDIT: Based on your update, you want to add pause to your first child bat file, then the code above before the call command in your parent bat file.
You should really edit your question to include the sequence of events you want from the comments because it causes a lot of confusion.
I'll try to piece together what you want:
Start job 1, creating a new window
Do not wait for job 1 to finish, instead wait 15 seconds
Start job 2, creating a new window
If you press Ctrl+C in either window it should stop the job and close the window
I assume that if one job is terminated before or after the other is started, the other still needs to start or continue to work.
First of all, the program that currently runs - in your case that seems to be maven - actually receives Ctrl+C and decides how it wants to act on it. I'm not sure if it does in the way you want it to.
Main.bat
echo Task-1:
"%comspec%" /c "C:\Users\user\bat\My_bat1.bat"
timeout 15 /nobreak
echo Task-2:
"%comspec%" /c "C:\Users\user\bat\My_bat2.bat"
I added a timeout command between the launches. It waits 15 seconds.
If you removed /nobreak it would also be possible to interrupt the wait with a keystroke launching task#2 early
I find that call sometimes introduces wierd errors in such cases so I prefer "%comspec%" /c. It does the same thing except it does not receive back environment variables set in child file which is fine in your scenario.
My_bat1.bat
start "" "%comspec%" /c "cd PATH & mvn -P dev & timeout -1 /nobreak"
I removed /wait which prevented the second mvn instance from starting until the first one is finished or terminated.
I replaced /k with /c and added timeout to pause execution in stead of /k.
The timeout -1 /nobreak statement causes command interpreter to wait indefinitely without a chance to stop it except for Ctrl+C.
You can remove /nobreak to allow it to be closed with any keystroke if mvn exited normally (if that ever happens)
"%comspec%" is the same as cmd.exe but is preferred if Microsoft ever decides to change command interpreter exe name. Empty "" before is required because start interprets first double-quoted string as a window name.
I assume there is a folder named PATH inside current folder because it is not a variable. Furthermore, variable %PATH% is reserved for executable/library search path list and must not be assigned some random value or used with cd command unless you really know what you are doing. See path /?.
I also used & instead of && to prevent window from closing if mvn crashes.
My_bat2.bat can be changed similarly.
So I wrote a batch file to open and close an application after 5 seconds (In Windows 7). Here is the code:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
#echo off
cd "C:\Users\owner\Downloads\"
Start "" /b ThrottleStop.exe
timeout /T 5 /nobreak >nul
taskkill /IM ThrottleStop.exe /F
exit
I will now give an instance of when in works correctly and when it does not:
Test 1: Run batch file by clicking on it.
Result: Command prompt Opens, code runs, the Application open and closes after 5 seconds. This test is a success.
Test 2: Place a shortcut to the same Batch file in the startup folder to execute upon each log in.
Result: When logged in the command prompt opens and appears to start running. However application NEVER opens, the batch file simply exits. This test is a failure.
Test 3: Set Task Scheduler to execute Batch file every login. When logged in the command prompt opens and appears to start running. However application NEVER opens, the batch file simply exits. This test is a failure.
Following Test 2 and 3 I tried running it manually again and it executes correctly. So the issue is only auto running it at startup. Is it possible that it needs a delay in it due to certain processes not booting up yet (on Windows side)?
If you want ThrottleStop to exit 5 seconds after it starts, add this line to the ThrottleStop.INI configuration file.
ExitTime=5
If you want to know how to properly use the Task Scheduler, follow the ThrottleStop Task Scheduler Guide exactly. There are options in the Task Scheduler that need to be disabled.
http://forum.notebookreview.com/threads/the-throttlestop-guide.531329/#post-6865107
Am trying to create a batch file, that would launch multiple programs. But unfortunately, things don't seem to work.
Kindly, find below my requirement:
Open InfluxDB server
Launch Grafana application.
Commands used in the batch:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
Start.cmd
timeout 5
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
grafana-server.exe
The above script, launches InfluxDB. But doesn't moves further.
Could you please suggest me, on how to proceed?
You need to use the call keyword to have control returned to the caller after invoking another batch script:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
call start.cmd
...
Should start.cmd run InfluxDB synchronously (i.e. not in the background) you need to launch it in a separate window:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start "InfluxDB" cmd /c start.cmd
...
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start InfluxDB
ping -n 6 127.0.0.1 > nul
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
start grafana-server
Edit the "start InfluxDB" and "start grafana-server" to be the correct exe names, without .exe
I am trying to create a batch file on a USB drive that will run an .exe, save its text to a .txt file, and then close the .exe.
I am currently running into a weird problem, only 5 of the 18 .exe's are actually saving their text to a file.
This is the convention I am using to complete my task:
start IEPassView.exe /stext Results/IEPassView.txt
taskkill /f /im IEPassView.exe
start MailPassView.exe /stext Results/MailPassView.txt
taskkill /f /im MailPassView.exe
start MessenPass.exe /stext Results/MessenPass.txt
taskkill /f /im MessenPass.exe
start RouterPassView.exe /stext Results/RouterPassView.txt
taskkill /f /im RouterPassView.exe
start ProtectedStoragePassView.exe /stext Results/ProtectedStoragePassView.txt
taskkill /f /im ProtectedStoragePassView.exe
start DialUpPassView.exe /stext Results/DialUpPassView.txt
taskkill /f /im DialUpPassView.exe
I have 18 of the above blocks in a row all calling different small programs and even though 5 of them actually save the files none of them save a .cfg file as they sometimes do. Any help would be greatly appreciated, thanks.
There are mainly 3 different types of executables:
A console application is reading from stdin or a file and writing to stdout or a file and outputs error messages to stderr.
The processing of a batch file is halted on starting a console application until the console application terminated itself. The correct term is therefore: calling a console application.
The exit code of the console application is assigned to environment variable ERRORLEVEL and can be also directly evaluated for example with if errorlevel X rem do something
Many *.exe in System32 directory of Windows are such console applications, like find.exe, findstr.exe, ping.exe, ...
A GUI (graphical user interface) application is started as new process which means the Windows command processor does not halt batch processing until the GUI application terminates itself.
It is not easily possible to get something from within a command process from such applications. GUI applications are designed for interacting via a graphical user interface with a user and not via standard streams or files with a command process.
A typical example for such applications is Windows Explorer explorer.exe in Windows directory.
A hybrid application supports both interfaces and can be therefore used from within a command process as well as by a user via GUI.
Hybrid applications are rare because not easy to code. The behavior of hybrid applications on usage from within a batch file must be find out by testing.
Example for such applications are Windows Registry Editor regedit.exe or shareware archiver tool WinRAR WinRAR.exe.
It is best to look on simple examples to see the differences regarding starting/calling all 3 types of applications from within a batch file.
Example for console application:
#echo off
cls
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 5
echo.
echo Ping finished pinging the own computer (localhost).
echo.
pause
The command processing is halted until ping.exe terminated itself which takes 4 seconds. There is no need for start or call for such applications, except the console application should be intentionally executed in a separate process.
Example for a GUI application:
#echo off
cls
%WinDir%\Explorer.exe
echo.
echo Windows Explorer opened and is still running!
echo.
pause
This batch file outputs the text and message prompt to press any key immediately after starting Windows Explorer indicating that Windows Explorer was started as separate process and Windows command processor immediately continued on the next lines of the batch file. So although Windows Explorer was started and is still running, the batch processing continued, too.
Example for a hybrid application:
#echo off
cls
"%ProgramFiles%\WinRAR\WinRAR.exe"
echo.
echo User exited WinRAR.
echo.
pause
This batch file starts WinRAR without any parameter (usually not useful from within a batch file) if being installed at all in default installation directory. But batch processing is halted until the user exited WinRAR for example by clicking on X symbol of the WinRAR´s application window.
But opening a command prompt window and executing from within the window
"%ProgramFiles%\WinRAR\WinRAR.exe"
results in getting immediately the prompt back in command window to type and execute the next command. So WinRAR finds out what is the parent process and acts accordingly.
Windows Registry Editor shows the same behavior. Executing from within a command prompt window
%WinDir%\regedit.exe
results in opening Windows Registry Editor, but next command can be immediately entered in command prompt window. But using this command in a batch file results in halting batch processing until GUI window of Windows Registry Editor is closed by the user.
Therefore hybrid applications are used from within a batch file mainly with parameters to avoid the necessity of user interaction.
Okay, back to the question after this brief lesson about various types of applications.
First suggestion is using
Result\TextFileName.txt
instead of
Result/TextFileName.txt
as the directory separator on Windows is the backslash character, except the executables require forward slashes as directory separator because of being badly ported from Unix/Linux to Windows.
Second suggestion is finding out type of application. Is command start really necessary because the applications don't start itself in a separate process and need user interaction to terminate itself?
Note: Command start interprets first double quoted string as title string. Therefore it is always good to specify as first parameter "" (empty title string) on starting a GUI or hybrid application as a separate process. On starting a console application as a separate process it is in general a good idea to give the console window a meaningful title.
And last if the started applications really would need user interaction to terminate, it would be definitely better to either start and kill them after waiting 1 or more seconds between start and kill or start them all, wait a few seconds and finally kill them all at once.
Example for first solution with starting and killing each application separately:
#echo off
setlocal
set TimeoutInSeconds=3
call :RunApp IEPassView
call :RunApp MailPassView
call :RunApp MessenPass
call :RunApp RouterPassView
call :RunApp ProtectedStoragePassView
call :RunApp DialUpPassView
endlocal
goto :EOF
:RunApp
start "" "%~1.exe" /stext "Results\%~1.txt"
set /A RetryNumber=TimeoutInSeconds + 1
%SystemRoot%\System32\ping.exe 127.0.0.1 -n %RetryNumber% >nul
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF
It is also possible to use timeout instead of ping for the delay if the batch file is only for Windows Vista and later Windows versions.
Example for second solution with starting all applications, wait some seconds and kill them finally all:
#echo off
call :StartApp IEPassView
call :StartApp MailPassView
call :StartApp MessenPass
call :StartApp RouterPassView
call :StartApp ProtectedStoragePassView
call :StartApp DialUpPassView
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul
call :KillApp IEPassView
call :KillApp MailPassView
call :KillApp MessenPass
call :KillApp RouterPassView
call :KillApp ProtectedStoragePassView
call :KillApp DialUpPassView
goto :EOF
:StartApp
start "" "%~1.exe" /stext "Results\%~1.txt"
goto :EOF
:KillApp
%SystemRoot%\System32\taskkill.exe /f /im "%~1.exe"
goto :EOF
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
goto /?
ping /?
set /?
setlocal /?
start /?
taskkill /?
See also the Microsoft article about Using command redirection operators.
PS: The last two batch code blocks were not tested by me because of not having available the applications.
I suggest killing all tasks at once, at the end of the very end, possibly after a timeout command with a amount of time appropriate to your system's speed. That may help the issue.