Bat, How to prevent environment variables leaking into parent process - batch-file

I have a bat file similiar to this
rem build.bat
rem add visual studio paths to env
call "%PROGRAMFILES(X86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
cl.exe blabla
After running it a couple of times I get the error The input line is too long. because vcvarsall.bat seems to append to the path variables every time I invoke the script
I invoke the script with build.bat
I thought that the environment variables should not be saved between runs. Is there any way to make the variables not leak into the calling cmd shell?

You should localize the environment of your batch file by putting setlocal at the beginning and endlocal at the end (the latter may be omitted as it executes implicitly on exiting the script).

You could start the build.bat in a child process.
cmd /c build.bat
Variables in the parent process aren't affected by a child process.

As far as I understand, environment variables don't get saved between runs, which is correct, but a "run" in this case is not the "run" of the batchfile, but of the command prompt in which you run the batchfile.
In my opinion, you have opened a command prompt. In there you launch the "build.bat" file, over and over again.
However, if you would launch the batchfile from outside of a command prompt (like double-clicking from the Windows explorer), this problem should not appear.

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.

Using call <file.bat> results in "sleep is not recognized as an internal or external command.."

I have a script that calls other commands in a for loop:
for %%x in (%CMDS::= %) do (
call C:\%%x %1%
echo "%%x complete"
)
However, running this results the console spitting out :
'sleep' is not recognized as an internal or external command,
operable program or batch file.
This is because the files i loop through and run have these commands in them. Why is it that if i run these files one by one they work, but when chained using call they don't? I can sleep in my terminal outside of this script..
Regards
Thanks to another answer, I solved this error by replacing sleep 5 in my .bat file with:
powershell -Command "& {sleep 5}"
Works fine now. Better still, also tested Stephan's suggestion:
timeout 5
Simpler, and shows a nice message like
Waiting for 0 seconds, press a key to continue ...
Note that some Windows versions require the /t option to define the time.
timeout /t 5
There is no sleep command in batch. That's why you are getting this error.
EDIT:
There is no sleep command in Windows CMD or Batch. BUT: as you can use the command in your console, I suppose there might be a script or a program called sleep. This script or program might be situated in your working directory or in some other directory included in your %PATH% variable. If this is the case, it's possible that your script gives you this error because of a path issue.
Say, you are in C:\SomeFolder and there is a sleep.exe in there. You are calling another script or command which changes the current directory to D:\AnotherFolder. Now another script or command tries to execute your mysterious sleep command assuming the working dir to be C:\SomeFolder but as you are in a different folder (D:\SnotherFolder) now, sleep can't be found. Further, when using call the variable scope of the calling script becomes also the scope for the called script. So it's also possible that variables are being overwritten by different scripts. Such a variable might contain the path to your sleep command. This could also cause an error.

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.

Batch script stops after first call to other batch script

I'm attempting to execute a batch script that currently looks like this:
D:
cd D:My Documents\FtpSolution\Test
getftp.bat
call delimconvert.exe
call convert-to-xls.bat
However this stops dead after getftp.bat has run.
What am I doing wrong? It's important that these commands all run sequentially.
Use call:
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
batch-parameters Specifies any command-line information required by the
batch program.
If you invoke other batch files without call then control is passed to them but not back again (which is what call changes).
use start command to launch it in a new window.
start /wait getftp.bat
Try using "Goto :EOF" rather than "exit" at the end of the batch file that you're calling - in your case, the getftp.bat file...
That's what fixed mine - tested on Win10 enterprise.

Resources