Batch file how to call another batch file at a specified label or call and immediately goto a certain label?

Chris Hayden picture Chris Hayden · Jul 20, 2012 · Viewed 8.3k times · Source

I am trying to figure out how file1.bat can call file2.bat at a specified label.

I figured I can do it like this:

File1.bat

:config
@echo off
:setvars
set labelmarker=labelmarker
call file2.bat
pause > nul
:EOF

File2.bat

if %labelmarker%==labelmarker goto label4
:label1
echo it won't work...
goto EOF
:label2
echo it must work!
goto EOF
:label3
echo it didn't work...
goto EOF
:label4
echo it works!
goto EOF
:EOF

This works. but I want to call a bat AND the Label from file1.bat. is it possible with a control character or ascii code or anything? like i tried

call file2.bat | goto label4 - doesn't work
call file2.bat > goto label4 - doesn't work
call file2.bat @label4 - doesn't work

Any help would be greatly appreciated.

Even if it involves extracting a specific label and contents to a new file would be ok.

Answer

Bali C picture Bali C · Jul 20, 2012

You could pass the label you want to go to as a parameter

Example scripts

First.bat

@echo off
set label=GOHERE
call Second.bat %label%
pause >nul

Second.bat

@echo off
goto %1
echo This line should be skipped
:GOHERE
echo Jumped here