Batch file to run other batch files with parameters - windows

I would like to have a run.bat file:
C:\> run functions
will run \exec\functions.bat
C:\> run patches
will run \exec\patches.bat
C:\> run
will run \exec\patches.bat and \exec\functions.bat (yes, in a predefined order)
How can I do so? Call batch command doesn't seem to be working
Thanks in advance :-)

call will work just fine:
rem if no parameter is specified run everything
if "%1"=="" goto :run_all
rem only run the batch file that was specified
call "execs\%1.bat"
goto :eof
:run_all
call "execs\patches.bat"
call "execs\functions.bat"

a_horse_with_no_name did the hard work - I just wrapped it a different way.
#echo off
rem if no parameter is specified run everything
if "%~1"=="" (
call "\execs\patches.bat"
call "\execs\functions.bat"
) else (
call "\execs\%1.bat"
)

Related

Please explain following outputs in Windows command prompt

I tried to customize windows command prompt with the following batch file.
#echo off
cls
:cmd
set /p "cmd=%cd%>"
%cmd%
goto cmd
So, when I open the batch file, it just takes my command into cmd variable and executes it and again prompts for a new command.
But the following command echo %cd% outputs only %cd%
Then I enabled delayedexpansion and used echo !cd! and got the desired output.
I think, because of the delayed expansion, cmd variable now holds echo c:\Users\Sourav\Desktop (am I correct?)
But I got confused when I tried to open the command prompt (not the batch file) and tried the following commands.
I thought, I will get c:\Users\Sourav\Desktop but I got !cd!. This contradicts my understanding of how echo !cd! is working in first case.
Why am I getting different output in the second case?
Can anyone suggest any improvement to the batch file, so that I can get desired output just using echo %cd% in first case?
you need another level of parsing. You can use call to do so:
#echo off
cls
:cmd
set /p "cmd=%cd%>>"
call %cmd%
goto cmd

How do I detect if script was CALL'ed or invoked directly, in Windows CMD.EXE shell?

I need to distinguish these two situations inside script.cmd:
C:\> call script.cmd
C:\> script.cmd
How can I determine if my script.cmd was invoked directly, or invoked in the context of using a CALL?
If it matters, this is on Windows 7.
#echo off
set invoked=0
rem ---magic goes here---
if %invoked%==0 echo Script invoked directly.
if %invoked%==1 echo Script invoked by a CALL.
Anyone know the "magic goes here" which would detect having been CALL'ed and set invoked=1?
At this moment, I see no way to detect it, but as a workaround you can always force the use of the sentinel.
#echo off
setlocal enableextensions
rem If "flag" is not present, use CALL command
if not "%~1"=="_flag_" goto :useCall
rem Discard "flag"
shift /1
rem Here the main code
set /a "randomExitCode=%random% %% 2"
echo [%~1] exit with code %randomExitCode%
exit /b %randomExitCode%
goto :eof
rem Retrieve a correct full reference to the current batch file
:getBatchReference returnVar
set "%~1=%~f0" & goto :eof
rem Execute
:useCall
setlocal enableextensions disabledelayedexpansion
call :getBatchReference _f0
endlocal & call "%_f0%" _flag_ %*
This will allow you to use the indicated syntax
script.cmd first && script.cmd second && script.cmd third
The posted code ends the script with a random exit code for testing. Execution will continue when the exit code is 0
NOTE: For it to work, at least in XP, it seems the call to the batch file MUST be the last code in the batch file
Check if the script's path is in the CMDCMDLINE variable. If not, then it was probably called.
In this example I use %CMDCMDLINE:"=/% to turn the quotes into forward-slashes (the FIND command can't search for quotes) and I echo it with <NUL SET/P="" so that certain characters in the file path (like ampersands) don't break the script.
<NUL SET/P="%CMDCMDLINE:"=/%" | FIND "/%~0/">NUL || (
REM Commands to perform if script was called
GOTO:EOF
)
::AND/OR
<NUL SET/P="%CMDCMDLINE:"=/%" | FIND "/%~0/">NUL && (
REM Commands to perform if script was NOT called
GOTO:EOF
)

Using the call function on MS-DOS to call all files in a directory with a certain extension

I have a code that calls specific files [.cmd files to be exact] using the call function, then echos a certain variable. This is highly inefficient due to the fact that the program needs to manually call each file. The current code, in case you need it is:
call afile.cmd
echo %Title%
call bfile.cmd
echo %Title%
pause > nul
[this is only the call part] Is there anyway to make it so it automatically calls all files with a .cmd extension, and displays all the %Title% variables without multiple
echo %Title%
functions?
I've tried using:
call *.cmd
call *.*
call *
just to see if those would work [since the * usually defines all files] but they didn't. Any suggestions for me?
setlocal enabledelayedexpansion
#echo off
For %%a in (*.cmd) do (
call "%%a"
echo !title!
)
pause

Rafer to the other batch in the in the same folder win 2008

I refer to other files in the same folder as the batch script by using this syntax:
CALL %0..\SecondBatch.cmd and this works with windows 2003 server but is not working with 2008 server. Any ideas are do welcome
Did you started the script as Admin (this will change the directory to system32)?Try with:
"%~dpfs0\SecondBatch.cmd" Thus you'll call the second .bat using the full path.
Proper way of calling another batch file in the same directory of the running batch should be
#echo off
setlocal enableextensions
rem This is first.cmd , Let's call second.cmd
call "%~dp0\second.cmd"
But there could be a problem. If first.cmd has been called with quotes, and inside first.cmd the current drive has been changed, the reference %~dp0, that should return the path of the current batch file, fails.
If this is your case, a construct of this kind can be used
#echo off
setlocal enableextensions
rem This is first.cmd , Let's call second.cmd
call :getScriptPath folder
call "%folder%\second.cmd"
exit /b
:getScriptPath returnVar
set "%~1=%~dp0"
goto :EOF

Running command-line in batch file

I have the following code which doesn't seem to be working properly - is someone able to assist with how to run command-lines in batch files
#echo off
set changeFrom=321
set changeTo=123
set origFile=config.txt
set newFile=config1.txt
test.bat %changeFrom% %changeTo% %origFile%>%newFile%
del %origFile%
ren %newFile% %origFile%
::end
I have a file "test.bat" which has code to replace strings in a file - but I Don't get how it can work ?
You need to use call to execute the second bat file from the first like this:
call test.bat %changeFrom% %changeTo% %origFile%>%newFile%
without call the first batch script will exit when the second one exits.

Resources