How do I keep DOS batch echo on for parent script? - batch-file

I have a DOS batch file that I want echoing turned on. However, this DOS batch file calls other batch files that then turn echoing off. I don't mind that the child batch files turn echoing off for themselves. But, is there any way for them not to affect the main script's echoing?

Run them with cmd /c . In separate CMD instance.
Instead of:
call otherScript.bat
Use:
cmd /C otherScript.bat
This is of course only relevant if we are talking about Windows NT batches.

Unfortunately its not possible. You would have to re-enable (#ECHO ON) echo after calling out to another batch file.

No. Hovewer I'm not sure what will happen if you try to redirect output of a child script to a different stream.

Related

How to create new commands in batch

Just like ipconfig, when typed in batch it executes a command, so how do I make a new command, without using CALL, and can be executed anywhere, not just that path.
So just like that this code-
#echo off
color fc
echo Example
echo :--:
pause
Can be executed by this command
EXEM
If you can help, thanks.
You can write your EXEM.BAT file as normal, then convert it to EXEM.EXE with something like this bat to exe converter.
You can then put it somewhere in your %Path% and it will run when running EXEM from CMD, without terminating execution.
Good luck!

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.

Batch File Ending Prematurely

I have a batch file with the following commands (to set up a compiler):
del Yylex.java
jflex scanner.flex
del parser.java
java -jar java-cup-11a.jar parser.cup
However, for some reason, after the conclusion of jflex scanner.flex, the batch script ends and command prompt closes. If I just run that command separately, this does not happen. Does anyone know what's wrong?
Is jflex a batch file?
If so, try
CALL jflex ...
or
start /wait "" jflex ...
(well, actually - give it a whirl anyway, can't hurt...)
When bat is asked to run another batch, it merely transfers control to that other batch and has no idea of where to return. CALL or START gives it a ticket home...

Why does calling a nested batch file without prepending "call" to the line exit the parent batch file?

I understand how to call nested batch files from within a parent file using the call command, as there are plenty of resources on that:
CALL
CALL (SS64)
Bat file termination
However, I don't understand why calling another batch file from another terminates the parent.
For a less abstract example, suppose I have a batch file that "links" together separate batch files, and I erroneously didn't prepend call to each line:
foo.bat
bar.bat
This would only execute foo.bat and then exit. To correctly execute both commands, I would have to prepend call before each statement:
call foo.bat
call bar.bat
Why does the first functionality still exist? Why hasn't it been changed? I noticed that call was introduced in MS-DOS 3.3, which was released in the late 1980s, so is this functionality still here for reverse compatibility?
I can't think of any (practical) usages of it, but perhaps I'm too used to "new" programming techniques.
DOS used simple text processing (back when you had things like FILES=20 in config.sys to allow 20 file handles), so opened the file, read the next line, closed the file, then executed the line just read. If the file called another, then the processing continued with that file, so only 1 file handle would be required for a batch file.
Until Microsoft put in the call command, there was no way to get back to the original file (without using tricks like giving the name of the previous file as a parameter, and using temporary files to let the original batch file know it had dome some processing, and could then GOTO the next part of the file).
As Sean Cheshire wrote, it's necessary for backward compatibility.
But starting a batch file from a batch file without using CALL does not terminate the parent!
It looks that way, as the parent normally will not executed further after the second batch exits.
But using a call before starting the second.bat, will show that the first batch isn't terminated.
parent.bat
echo parent.bat
call :myLabel
echo back in parent.bat main
exit /b
:myLabel
second.bat & echo back in parent.bat
exit /b
second.bat
echo second.bat
exit /b
I use here the the secpond.bat & echo back ... to avoid another bug/feature of cmd.exe.
If you use second.bat without any extras it will start second.bat AND jump to the label :myLabel in second.bat!
Call is basically saying "go execute this other batch file, and then come back here and continue". It has been there since DOS 3.3 or so, and if it were removed now would break all backward-compatibility (which is why people are still using batch scripts). It can also be used to branch to :link locations.
For info on the use and syntax (for future reference for others), you can see this MS TechNet link
If you need new functionality, use CMD scripts or PowerShell scripts instead.

Force bat file to use non default cmd.exe

I'm writing a .bat file to handle some script generation automatically so I don't have to type in half a dozen command arguments each time I want to run it.
I have to run a vb script from the batch file
#call ..\Database\scripts\runscriptupdates.vbs
However the script will only run if using the the command prompt from
C:\Windows\SysWOW64\cmd.exe
By default the bat file uses the cmd.exe in system32
C:\Windows\System32\cmd.exe
Is there a way to force the batch file to use this cmd.exe to run the vbs file? I've been trawling the web for about an hour now and haven't found anything which helps (so far).
I've tried running the syswow64 with "start ..." however it doesn't seem to take the arguments after it.
Many thanks, Neil
You can try:
%windir%\SysWoW64\cmd.exe /c mybatch.bat
This will run the batch itself from a 32-bit command prompt. Thus, the call to your vbs will also be coming from a 32-bit command prompt.
I also had this problem, and I found the way to solve it.
You just need to change System Variables.
Go to Control Panel » System » Advanced System Settings » Environment Variables.
Find the variable ComSpec, then just click Edit... and change the path to "C:\Windows\SysWow64\cmd.exe"
Try typing this one line in your batch file.
%windir%\SysWoW64\cmd.exe /c ["]cscript [script name] [host options] [script arguments]["]
Where:
script name is the name of the script file, including the file name extension and any necessary path information.
host options are the command-line switches that enable or disable various Windows Script Host features. Host options are always preceded by two slashes (//).
script arguments are the command-line switches that are passed to the script. Script arguments are always preceded by one slash (/).
Example:
%windir%\SysWoW64\cmd.exe /c "cscript VoltageDrop.vbs /"Campbell.sin" "L08""
Note: In this line I do not pass any host options. This command will execute the string,
cscript VoltageDrop.vbs /"Campbell.sin" "L08"
as a command in the 32-bit command prompt.

Resources