Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Call .bat and go to label?  (Read 3811 times)

0 Members and 1 Guest are viewing this topic.

JRobinson

    Topic Starter


    Rookie
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Call .bat and go to label?
« on: January 28, 2017, 01:52:46 PM »
I googled it, thought I found a solution but it doesn't seem to work. I think I saw a thread on this a while back but I can't find it (the search never seems to work for me on the forum here for some reason, I get "no results" no matter what I search for). I'm using Win7 if that makes a difference. I found something that said extensions had to be enabled for this to work then checked my registry according to another site and determined they are enabled. I also tried setlocal enableextensions at the head of test.bat, that didn't work either.

I made these two test batch files:

test.bat
Code: [Select]
@echo off

cls
echo.
echo.
echo.
echo     Make a selection
echo.
echo.
echo     [1] selection 1
echo     [2] selection 2
echo     [3] exit
echo.
set /p selection=
if %selection%==1 call test1.bat one
if %selection%==2 call test1.bat two
if %selection%==3 exit


test1.bat
Code: [Select]
@echo off
echo.
echo.
echo.
echo this shouldn't ever show

:one
echo.
echo.
echo.
echo this is selection 1
pause
call test.bat

:two
echo.
echo.
echo.
echo this is selection 2
pause
call test.bat


"this shouldn't ever show" shows every time and if I choose selection 2 it shows "this shouldn't ever show" and then tells me I'm seeing the stuff under label :one rather than :two indicating that my call command is merely calling test1.bat and ignoring the labels entirely. I've tried call test1.bat :one (note the colon) and I tried setting "one" as a variable (set selection1=one) and then call test1.bat %selection1%, no luck either way (not that I was really expecting a different result, but...). What am I doing wrong? If you copy those and run test.bat do you get the same result?

Seems like it should be so simple! :)

Jim

Salmon Trout

  • Guest
Re: Call .bat and go to label?
« Reply #1 on: January 28, 2017, 04:38:44 PM »
Label names are only valid inside the script. You can't just add the label name after the batch name and expect the script to know what you mean. If you want to pass the label name as a single parameter from the command line (or another script), you could insert:

goto %1

after the @echo off line.

Doing it this way means you must always pass a valid label name or the script will halt with an error.

Or you can do IF tests, e.g.

if "%1"=="one" goto one
if "%1"=="two" goto two
...etc
(you need a final goto to catch a wrong or missing label)


By the way, looking at your example script:

Code: [Select]
:one
pause
call test.bat

:two
echo.

You do realise that this code will always go to :two after test.bat has finished? After a label section, you need to explicitly goto (somewhere) otherwise execution will continue at the next code line, skipping any labels, which may not be what you wanted it to do.
« Last Edit: January 28, 2017, 05:00:01 PM by Salmon Trout »

JRobinson

    Topic Starter


    Rookie
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Call .bat and go to label?
« Reply #2 on: January 29, 2017, 12:02:15 AM »
I really appreciate the help Salmon Trout, I didn't know about %1. That's perfect, did the trick!

You do realise that this code will always go to :two after test.bat has finished?

I'd forgotten about that, but as it turned out it worked OK with this particular example since test.bat would either call test1.bat again or exit depending on the selection chosen. I can see the error of my ways though if set /p didn't pause test.bat waiting for user input it would finish and then go back to test1.bat :two. Here's what I came up with, I guess going to :EOF after the pause would terminate test1.bat so it doesn't return after test.bat finishes (and :two doesn't need it because we're at EOF when it returns to test.bat anyway)?

test1.bat
Code: [Select]
@echo off

goto %1

echo.
echo.
echo.
echo this shouldn't ever show

:one
echo.
echo.
echo.
echo this is selection 1
pause
::call test.bat
goto :EOF

:two
echo.
echo.
echo.
echo this is selection 2
pause


That seems to do what I wanted to do anyway, still working on catching wrong or missing labels.

Again, I appreciate the help. :)

Jim

Salmon Trout

  • Guest
Re: Call .bat and go to label?
« Reply #3 on: January 29, 2017, 01:38:45 AM »
Here's what I came up with, I guess going to :EOF after the pause would terminate test1.bat so it doesn't return after test.bat finishes (and :two doesn't need it because we're at EOF when it returns to test.bat anyway)?
Perfect! Sometimes with labels instead of the implicit :eof I put a final one called :end or whatever and have something like this

:end
echo Script finished
pause