Code: Select all
@echo off
Cls
Call other.bat hello
Code: Select all
::other.bat
@echo off
Cls
Echo hello
:hello
Good bye
Moderator: DosItHelp
Code: Select all
@echo off
Cls
Call other.bat hello
Code: Select all
::other.bat
@echo off
Cls
Echo hello
:hello
Good bye
Code: Select all
@echo off &setlocal
call "other.bat"
call "other.bat" test
call "other.bat" hello
call "other.bat" notexistinglabel
pause
Code: Select all
if "%~1" neq "" (call :%~1&exit /b)
echo No argument passed.
exit /b
:hello
echo Hello World!
exit /b
:test
echo This is a test.
exit /b
Code: Select all
@echo off
call :test
echo Back from other.bat :test
call :hello
echo Back from other.bat :hello
exit /b
:test
:hello
other.bat
echo This will not be reached
exit /b
Code: Select all
@echo off
echo Calling :test and :hello labels in other.bat file:
(
ren "%~NX0" main.bat & ren other.bat "%~NX0"
call :test
echo Back from other.bat :test
call :hello
echo Back from other.bat :hello
ren "%~NX0" other.bat & ren main.bat "%~NX0"
)
echo Continue in original file
Code: Select all
@echo off
Cls
Echo hello
:hello
Echo Good bye
exit /B
:test
echo This is a test.
exit /B
jeb wrote:But you can use also a cmd.exe bug.
Aacini wrote:jeb wrote:
But you can use also a cmd.exe bug.
Why you call this a "bug"? If another Batch file is executed with no CALL command the process ends at the second Batch file; this is the standard behavior of Batch files and the reason because CALL command was implemented.
Code: Select all
call :myLabel
exit /b
:myLabel
REM This starts second.bat AND goto to :myLabel in second.bat
second.bat
Code: Select all
set "anonymous=/?"
CALL :%%anonymous%%
second.bat
SIMMS7400 wrote:Just curious, what would be the purpose of calling a label from another bat? Why not just include it in current bat? Is the concern you'd prefer not to repeat code?
Thanks, I'm just a stupid guy trying to learn!
SIMMS7400 wrote:Just curious, what would be the purpose of calling a label from another bat? Why not just include it in current bat? Is the concern you'd prefer not to repeat code?
Thanks, I'm just a stupid guy trying to learn!
Code: Select all
setlocal
set prompt=$g$s
:: rem - maybe you would like to test this Script?
@echo off
:: continous line and 2nd :label
call :myfunc1 D:\func with all Parameters 1
:myfunc1 ^
%*
rem < this line does not excute
echo script goes on
echo ...
set test
:: one empty line and one Label
call ^
:myfunc2 ^
D:\Func with all Parameters 2
echo rem this line execute
echo script goes on
echo ... ...
set test
:: one label; one space; all Argument
call ^
:myfunc3 D:\func with all Parameters 3 ^
%*
echo this execute
echo script goes on
echo ... ... ...
set test
pause &exit /b
rem Favorit is
(
call ^
:myfunc1 ^
D:\func with all Parameters 1
echo end script
set test
)
Code: Select all
:: D:\func.cmd
:myfunc1
echo %*
set test=1
echo on
exit /b
:myfunc2
rem %*
set test=2
echo off
exit /b
:myfunc3
echo %*
set test=3
echo this is the last
exit /b
Code: Select all
call ^
:myFunc1 func.bat parameter
call :myfunc1 D:\func with all Parameters 1
:myfunc1 ^
%*
rem < this line does not excute
Code: Select all
:myfunc1 ^
rem < this line does not excute