Call label from other batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Call label from other batch file

#1 Post by batchcc » 31 Dec 2015 09:14

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Call lable from other batch file

#2 Post by aGerman » 31 Dec 2015 10:34

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Call lable from other batch file

#3 Post by jeb » 31 Dec 2015 11:29

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"

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Call lable from other batch file

#4 Post by Aacini » 31 Dec 2015 13:36

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Call label from other batch file

#5 Post by jeb » 01 Jan 2016 06:17

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

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Call label from other batch file

#6 Post by batchcc » 01 Jan 2016 10:39

Thank you everyone for helping me your solutions all worked very well. :) -Batchcc

LotPings
Posts: 10
Joined: 26 Oct 2016 22:45

Re: Call label from other batch file

#7 Post by LotPings » 08 Dec 2016 05:32

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.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Call label from other batch file

#8 Post by SIMMS7400 » 08 Dec 2016 08:25

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!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Call label from other batch file

#9 Post by Squashman » 08 Dec 2016 09:11

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.

LotPings
Posts: 10
Joined: 26 Oct 2016 22:45

Re: Call label from other batch file

#10 Post by LotPings » 08 Dec 2016 10:19

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

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Call label from other batch file

#11 Post by pieh-ejdsch » 11 Dec 2016 16:06

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

LotPings
Posts: 10
Joined: 26 Oct 2016 22:45

Re: Call label from other batch file

#12 Post by LotPings » 12 Dec 2016 16:02

That's a clever trick with a caret line continuation char to have the call using it's self label and args.

Cheers LotPings

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Call label from other batch file

#13 Post by jeb » 13 Dec 2016 05:27

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.

Post Reply