Start a batch file and close Cmd once it is finished - batch-file

I have my application spread over multiple directories, each containing a part (e.g. web frontend, mobile application, administration, middleware, backend, ...).
In each of the directories I have one part, and a file compile.cmd which compiles that part and looks roughly like this:
#ECHO OFF
compiler prepare thisPart
compiler compile thisPart
copy resultingFile1 ../bundleDirectory
...
copy resultingFileN ../bundleDirectory
pause
The "pause" is so I can check the compiler output, whether compile failed and which error messages occurred, and then close the window with a single keystroke (mostly, space key).
I now want to have a batch file that calls all these batch files for different application parts in parallel, so I guess I have to open a new shell window for each.
So I wrote CompileAllParts.cmd like this:
cd part1
start "Compile part 1" compile.cmd
cd ../part2
start "Compile part 2" compile.cmd
cd ../part3
start "Compile part 3" compile.cmd
Positive is that I can influence the window title, but the drawback is that new Cmds are spawned which do not automatically close.
This is also part of the documentation of start:
If command is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.
Is there a hidden parameter to explicitly disable this behaviour?

combine start with cmd. First to set title and working folder, second to use /C and execute the command:
start "Compile part 1" /d "part1" cmd /c "d:\proper folder\compile.cmd"

Related

Run a different batch file when one coses

I have 2 batch files (1.bat and 2.bat). I want 2.bat to run when 1.bat closes. Is that possible?
Or is there a way to close a batch file when a batch file closes. Here is my code which doesnt work:
cd C:\xampp
start apache_start.bat
cd C:\Users\MinecraftServer\Desktop\1.12.2MinecraftServer
MC1.12.2Start.bat
taskkill /F /IM cmd.exe /T
I have a minecraft server running with a .bat file and a web server running off of a .bat file (using xampp, its used for prtg monitorng).
start will create a completely independent process and continue immediately to your next batch command. That independent process may do a job and terminate, or may say open an application like notepad and wait until the notepad is closed. Regardless, a straight start command will not stop the batch that it is in - the next step will be executed, whether or not the process that has been started has terminated.
You can also add the /wait switch to start. If this switch is used, the batch will wait until the started process terminates.
Preferred syntax for start is
start /wait "window title" executable parameters...
where "window title" may be an empty, but not absent string. Including this as the first parameter is preferred because the first quoted string in a start command is used as a window title for the started process, and thus start may not act as expected where the executable or any of its parameters is quoted.
call executablename... will wait for that executable to terminate before progressing to the next step. This may or may not be what is desired.
MC1.12.2Start.bat in your code will SWITCH to that batch and remaining batch lines will be ignored.
Which may be fortunate in this case, since as you've coded it, the apache_start.bat may or may not have completed when the taskkill command is run, killing all cmd.exe sessions.

cmd new window store output

I want to open a new command prompt, run an program and save the output (which is right now displayed in the command prompt) in a logfile.
I used this command so far:
cmd /c start "window title" "C:\Program Files\app.exe"
normally I can use
"C:\Program Files\app.exe" >out.txt
to save what is written on the command prompt in a file.
With the need, that the programm is executed in another window, I'm struggling to set the output correctly.
Why do I need the extra window?
--> The program will be called several times. I need a license for that, I'm limited there. If the program is called in the same window, after 3 three times an error occurrs, telling me, that I use to many license at the same time.
With extra opening and closing windows this "license problem" is solved.
But the I cannot find the solution for the output then.
Lua is tagged, since this command is embedded in Lua's os.execute()
You should examine start /? for the full syntax of the command. You need the following elements:
the start command
"window title" (can be "" if you don't wish to specify)
/d "working directory"
"command name"
"command arguments"
... where each token after start is quoted. Example:
start "" /d "C:\Program Files\appdir" "app.exe" ">%userprofile%\Desktop\out.txt"
Use the start command's expected argument structure to pass the output redirection as an argument. Pass it quoted so the cmd interpreter knows you want the output of app.exe redirected, as opposed to the output of the start command (which doesn't natively provide any useful output data).
Windows command line has wicked rules about quoting :-)
This code works as you need:
os.execute([["start "window title" cmd /C ""C:\Program Files\app.exe" > "C:\my logs\log.txt"""]])
I suppose you are looking for something like,
start "window title" "C:\Program Files\app.exe > out.txt"
The redirection is inside the quotes, else it would fetch the output of the start command itself (that output is empty).

batch file to exe to auto run when computer start

I am a new programmer, i have created a batch file to open a website but i want that batch file to run when the computer starts.
Here's the code:
#echo off
:top
start iexplore.exe http://www.website.com
timeout 3
goto top
While i do think Arescet's answer will work, i am more in favor of using Windows' Task Scheduler.
Simply create a new task :
Assign it's trigger to be At Startup :
and add a new action to Start a program giving it the path to your batch file:
I believe this is a cleaner approach which also provides you with logging and history should you decide to do more things in your batch file later on
Type run into windows search, type shell:startup in the prompt, press enter, Then, create a shortcut of your program, and move the shortcut into the startUp folder.
On note of your code, the start command should have a blank title as "", and quotes around the application name and parameters passed, like:
start "" "iexplore.exe" "http://www.website.com"

Is there a way to run vcvars32.bat every time I start a cmd?

I'm using cl in cmd and having to run vcvars32.bat every time I open a cmd window is really a pain in the axx. Can anyone offer a way of running it automatically?
From cmd /?:
If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
You therefore could add vcvars32.bat to one of those AutoRun registry values to have it executed for every cmd.exe instance (except when /D is explicitly specified, of course).
However, be forewarned that doing this could result in other weird side-effects (for example, it could cause other .bat/.cmd scripts to be run in an environment that they aren't expecting).
A workaround that works for some people is to write a batch file and call it A.BAT and make a.bat launch vcvars32.bat. Put a.bat on the path and then it's a matter of opening the cmd prompt and typing a and enter and voila, you're set!
way old, but the easiest way to do this with, say, a shortcut created on your TaskBar is to modify your shortcut (in %appdata%\microsoft\internet explorer\quick launch\user pinned\taskbar, or thereabouts) so the target is:
%windir%\system32\cmd.exe /k vcvars32.cmd
that'll do exactly what you're looking for. The /k tells it to execute the string but keep the window open (string being your batch file). You can either put vcvars32 somewhere in your path, or specify the whole path to vcvars32.
You can use the script in http://www.alteridem.net/2010/09/02/visual-studio-2010-command-prompt-here to make it so when you right-click a folder in explorer the option shows up. After downloading and extracting the zip file you can modify the .inf to point to the correct path to your particular VS version (and change the displayed name if desired). Note the comment on the page about having to rename the file if you are running 64-bit Windows.

Run batch files sequentially

I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.

Resources