Page 1 of 1

Call label from other batch file

Posted: 31 Dec 2015 09:14
by batchcc
How can I call a label in a batch file from a different batch file.

Code: Select all

@echo off 
Cls
Call other.bat hello

Code: Select all

::other.bat
@echo off
Cls
Echo hello
:hello
Good bye

Re: Call lable from other batch file

Posted: 31 Dec 2015 10:34
by aGerman
Try

Code: Select all

@echo off &setlocal
call "other.bat"
call "other.bat" test
call "other.bat" hello
call "other.bat" notexistinglabel
pause



other.bat

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

Regards
aGerman

Re: Call lable from other batch file

Posted: 31 Dec 2015 11:29
by jeb
The solution of aGerman is the safe and readable variant.

But you can use also a cmd.exe bug.

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


This code goes directly to the label in the "other.bat" file.
When the other.bat exits than it returns after the "call :test" or "call :hello"

Re: Call lable from other batch file

Posted: 31 Dec 2015 13:36
by Aacini
You may use this trick that allows to "switch the context" of the running Batch file, so all labels in the other.bat files becomes "reachable" to it:

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

other.bat:

Code: Select all

@echo off
Cls
Echo hello

:hello
Echo Good bye
exit /B

:test
echo This is a test.
exit /B

This method have the advantage that the other.bat file does not need to be modified; however (as others indicated in the given link), this method is not "safe" because if a run-time error happen when the context is changed, the files remain renamed.

___________________________________________________

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.

Antonio

Re: Call label from other batch file

Posted: 01 Jan 2016 06:17
by jeb
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.


As it's not normal, that starting another batch will implicitly also executes a goto to the last called label.

Main.bat

Code: Select all

call :myLabel
exit /b

:myLabel
REM This starts second.bat AND goto to :myLabel in second.bat
second.bat


Btw.
You can produce funny effects with an anonymous call and start a second batch.

Code: Select all

set "anonymous=/?"

CALL :%%anonymous%%
second.bat

Re: Call label from other batch file

Posted: 01 Jan 2016 10:39
by batchcc
Thank you everyone for helping me your solutions all worked very well. :) -Batchcc

Re: Call label from other batch file

Posted: 08 Dec 2016 05:32
by LotPings
Just to mention it, the behaviour and possible use as a batchlibrary is at least 11 years old. In the newsgroup alt.msdos.batch.nt is a thread (with foxidrive R.I.P. taking part) describing this. Since then I'm actively using it. THe bug/feature is constantly supported by all windows versions up to the latest.

Re: Call label from other batch file

Posted: 08 Dec 2016 08:25
by SIMMS7400
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!

Re: Call label from other batch file

Posted: 08 Dec 2016 09:11
by Squashman
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!


You just answered your own question. If you have a library of functions sitting in a another file you don't have to repeat the code.

Re: Call label from other batch file

Posted: 08 Dec 2016 10:19
by LotPings
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!

Yes one benefit is space saving, but there is more. Even if copied to the actual batch, using a set of args the code is reusable.

I keep the subs in a subdirectory 'sub' as single cmd files and when changing/adding something I automatically regenerate the batchlib, the stub file and also a cliplibrary for my favorite text/programing editor Textpad.
This way it's a matter of seconds to click together the template for a new batch.

The base of the library has been https://ritchielawrence.github.io/batchfunctionlibrary/ See two subs/functions here

Re: Call label from other batch file

Posted: 11 Dec 2016 16:06
by pieh-ejdsch
to make call in a functional library and to write continuously the executive Batch, the label from call becomes at the same time the label of the called Label included of the parametres. The resolution of the arguments does the rest.
Therefore one comes by a huge number to a short one appeal.
These versions also function within parenthesis and ForLoops
This means a Label within parenthesis is able or is also called.


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


Phil

Re: Call label from other batch file

Posted: 12 Dec 2016 16:02
by LotPings
That's a clever trick with a caret line continuation char to have the call using it's self label and args.

Cheers LotPings

Re: Call label from other batch file

Posted: 13 Dec 2016 05:27
by jeb
It's a clever trick to reuse the label, but it has the drawback, that the complete file will always be scanned for the label.
The parser will search for the label from the current file position to the end of file and then begin from the beginning of the file up to the current file position.

Code: Select all

call ^
:myFunc1 func.bat parameter

When the parser fetch this line the current file position is just behind the "parameter"


One comment to this part
call :myfunc1 D:\func with all Parameters 1
:myfunc1 ^
%*
rem < this line does not excute


The REM line won't be executed, as long as %* is empty, as then the label consumes the REM line with a line feed character, as the code is equivalent to

Code: Select all

:myfunc1 ^

rem  < this line does not excute


But when %* has some content (test.bat param1) the REM line will be executed again.