SS64 Discussion Forum

You are not logged in.

#1 2009-11-15 15:20:41

insthink
8088
Registered: 2009-10-24
Posts: 51

Question - Goto <currentlabel> / Goto :eof

First question
I have a LOT of subroutines that will have several criteria making it loop back on itself [return to label top]

It's getting annoying as most of those subroutines are copy/paste format [I just modify the middle and label name].
Hence, programming errors constantly slip in, where I forget to change the "goto label" to the current label's name.

Is there some sort of variable that reports the last label called? hence no need to change "goto <currentlabel>" every time?

Last question

I know that failing to close a called subroutine with "goto :eof" will cause a lot of trouble.

But I also have a lot of called batches that don't end with "goto :eof", they simply end [reach the bottom]
After reading the SS64 GOTO article, I have become worried.

To exit a batch script file or exit a subroutine specify GOTO:eof

I have become afraid of future potential errors if it is REQUIRED to end with "goto :eof"
A lot of my CALLED batches, CALL other batches.
As long as those CALLED batches reach the bottom, will there be a problem?

Bonus question, I'm guessing there's no difference between

goto:eof

and

goto :eof

???


thanks in advance!

Last edited by insthink (2009-11-15 15:22:02)

Offline

#2 2009-11-15 17:42:47

carlos
PDP-11
Registered: 2008-11-03
Posts: 85

Re: Question - Goto <currentlabel> / Goto :eof

goto:eof
or
goto :eof

is the same instruction. The parser of cmd.exe made that it works.

A note:
goto :eof is a command enabled with extensions enabled.
internally command exit /b call to goto :eof
All this is with extensiones enabled

In the labels for gotos cmd.exe add a : to end of label.

For example:

goto :ini
is the same that goto :ini:

internally cmd.exe add a : to the end.

cmd.exe search the string :EOF for end the batch script if extensions are enabled.

Example:
View differences for eof :eof: and :eof

@echo off
setlocal enableextensions

goto eof
rem this jump down because eof is not begin with :

:EOF
echo this is a label called eof
pause
goto :eof
rem this end the batch script

similar:

@echo off
setlocal enableextensions

goto :eof:
rem this jump down because eof is begin with : and end with :

:EOF
echo this is a label called eof
pause
goto :eof
rem this end the batch script

The only valid for exit routine is :EOF


In others labels for example for label called mylabel this is the same:

goto mylabel
goto :mylabel
goto :mylabel:
goto:mylabel
goto:mylabel:

In windows nt the maximum lenght of labels are 10 and in older than is 8.

Greetings.

Last edited by carlos (2009-11-16 09:34:42)

Offline

#3 2009-11-17 08:21:19

insthink
8088
Registered: 2009-10-24
Posts: 51

Re: Question - Goto <currentlabel> / Goto :eof

I guess that sort of answers my question as to whether it was fine or not wink  As long as it jumps down to the bottom without hitting anything along the way that it wasn't supposed to go through.

Offline

#4 2009-11-18 05:29:41

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 701

Re: Question - Goto <currentlabel> / Goto :eof

It doesn't matter how it gets there, the script will exit or subroutine will end when it reaches the end of file (EOF).  The "goto :EOF" function is simply a shortcut to that.

You can use "exit /b #" as an alternative way to exit subroutines, setting the "errorlevel" in the process.

Last edited by bluesxman (2009-11-18 07:14:21)

Offline

#5 2009-11-18 10:47:04

avery_larry
IA-32
Registered: 2007-07-11
Posts: 250

Re: Question - Goto <currentlabel> / Goto :eof

For the current label thing -- not really, but you could use a variable to specify the label -- that might do what you're trying to do:

echo off
set currentlabel=one
call :%currentlabel%
set currentlabel=two
call :%currentlabel%
goto :eof

:one
echo Here's a subroutine copied/pasted from standard code
echo the currentlabel variable must be defined correctly prior to calling this subroutine.
if not defined currentlabel echo currentlabel must be defined && goto :eof
:onemiddle
echo Here's the middle of the subroutine
echo note it's the subroutine label with middle added
echo Now you can use the currentlabel variable to specify different "modular"
echo variables, labels, etc.
if defined finished%currentlabel% goto :eof
set finished%currentlabel%=yes
echo You can use a generic goto function to the middle of this subroutine
goto %currentlabel%middle

:two
echo Here's another subroutine from standard code
if not defined currentlabel echo currentlabel must be defined && goto :eof
:twomiddle
echo continuing the theme of a generic middle label
if defined finished%currentlabel% goto :eof
echo You can do stuff here in a loop.
set finished%currentlabel%=doesn't matter as long as it's defined
goto %currentlabel%middle

Offline

#6 2009-11-18 12:42:33

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 701

Re: Question - Goto <currentlabel> / Goto :eof

Not sure if this is useful to you, but you can find the last label called while you're still in a subroutine using the "%0" parameter, and take actions based on that.  NB - "goto" does not set "%0", which can be exploited.

For example:

@echo off

echo:call :label1
call :label1

echo:call :label2
call :label2

echo:call :label3
call :label3

echo:call :label4
call :label4

pause
goto :EOF

:label1
echo:some actions [1]

:label2
echo:some more actions [2]

if "%0" EQU ":label1" (
    echo:additional actions [2]
    goto :label4
)
if "%0" EQU ":label2" (goto :EOF)

:label3
echo:yet more actions [3]

:label4
if "%0" EQU ":label4" (
    echo:further actions [4]
) ELSE (
    echo:different further actions [4]
)

goto :EOF

Last edited by bluesxman (2009-11-18 12:43:00)

Offline

#7 2009-11-18 14:34:20

insthink
8088
Registered: 2009-10-24
Posts: 51

Re: Question - Goto <currentlabel> / Goto :eof

avery_larry wrote:

For the current label thing -- not really, but you could use a variable to specify the label -- that might do what you're trying to do

I did that for a while, but it became more of a hassle as I'd have to do "set %currentlabel%=labelname"

In other words, it still required me to specify the label name for every label looping on itself.


bluesxman wrote:

Not sure if this is useful to you, but you can find the last label called while you're still in a subroutine using the "%0" parameter, and take actions based on that.  NB - "goto" does not set "%0", which can be exploited.

I dont know how you'd get %0 to = currentlabel

I did a quick test and %0 only results in the file name within a subroutine.



I guess I'll stick to typing the label name for every "goto currentlabel"

Last edited by insthink (2009-11-18 14:35:11)

Offline

#8 2009-11-18 15:43:37

avery_larry
IA-32
Registered: 2007-07-11
Posts: 250

Re: Question - Goto <currentlabel> / Goto :eof

blues is right.  If you call a label instead of using goto, then %0 is the label.  I didn't realize that worked.  Of course, that doesn't work with a goto command.

Perhaps you can show us an example.  I still think using a variable for the label sounds like what you're after -- but if that's more hassle, then perhaps the application isn't very difficult.

Offline

#9 2009-11-18 16:47:54

insthink
8088
Registered: 2009-10-24
Posts: 51

Re: Question - Goto <currentlabel> / Goto :eof

avery_larry wrote:

blues is right.  If you call a label instead of using goto, then %0 is the label.  I didn't realize that worked.  Of course, that doesn't work with a goto command.

Perhaps you can show us an example.  I still think using a variable for the label sounds like what you're after -- but if that's more hassle, then perhaps the application isn't very difficult.

ahh... call, yes I see....
good to know, but would require rearranging a LOT of stuff to use call instead of goto tongue

but good to know for future additions!
Thanks!

Offline

#10 2009-11-18 23:41:19

sarbjitsinghgill
PDP-11
From: TORONTO CANADA
Registered: 2007-12-08
Posts: 79
Website

Re: Question - Goto <currentlabel> / Goto :eof

insthink wrote:
avery_larry wrote:

blues is right.  If you call a label instead of using goto, then %0 is the label.  I didn't realize that worked.  Of course, that doesn't work with a goto command.

Perhaps you can show us an example.  I still think using a variable for the label sounds like what you're after -- but if that's more hassle, then perhaps the application isn't very difficult.

ahh... call, yes I see....
good to know, but would require rearranging a LOT of stuff to use call instead of goto tongue

but good to know for future additions!
Thanks!

Call is better choice anyway. Most of my batch scripts have worked with goto , but still any script I rer-use I do prefer to change goto to call. This provides more choices and better control.

Sarbjit Singh Gill


Sarbjit Singh Gill dba
IBM certified database application developer

Offline

Board footer

Powered by FluxBB