Home All Groups Group Topic Archive Search About

How to make goto when nowhere to go ?

Author
13 Jun 2009 2:36 AM
Synapse Syndrome [KGB]
(NT shell scripting)

Say I have a goto parameter command -

goto :%parm%

If the batch label does not exist, the script ends with the error:

"The system cannot find the batch label specified - whatever"

I do not want the error message displayed, and I want the script to go and
do something else instead.  If I try..

goto :%parm:~2% || goto :error

...that does not work.

Any solution?

Cheers

ss.

Author
13 Jun 2009 3:46 AM
Al Dunbar
Show quote Hide quote
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:eG0tC$86JHA.1196@TK2MSFTNGP03.phx.gbl...
> (NT shell scripting)
>
> Say I have a goto parameter command -
>
> goto :%parm%
>
> If the batch label does not exist, the script ends with the error:
>
> "The system cannot find the batch label specified - whatever"
>
> I do not want the error message displayed, and I want the script to go and
> do something else instead.  If I try..
>
> goto :%parm:~2% || goto :error
>
> ..that does not work.
>
> Any solution?

NT shell scripting, on what O/S: nt3.51, nt4, w2k, w2k3, w2k8, xp?

I don't think you can trap an attempt to go to an invalid label. The
alternative is to check the value of the parm variable to see if it is a
valid label. One way:

    do %%L in (label1 label2 label3) if "%parm%" EQU "%%L" goto:%parm%

Another way would be to use FIND or FINDSTR to search the batch file for
instances of ":%parm%". Trouble is if there was a "label called ":whatsit"
and the value of parm was "what"...

/Al
Author
13 Jun 2009 1:06 PM
T Lavedas
Show quote Hide quote
On Jun 12, 11:46 pm, "Al Dunbar" <aland***@hotmail.com> wrote:
> "Synapse Syndrome [KGB]" <syna...@NOSPAMsyndrome.me.uk> wrote in messagenews:eG0tC$86JHA.1***@TK2MSFTNGP03.phx.gbl...
>
>
>
> > (NT shell scripting)
>
> > Say I have a goto parameter command -
>
> > goto :%parm%
>
> > If the batch label does not exist, the script ends with the error:
>
> > "The system cannot find the batch label specified - whatever"
>
> > I do not want the error message displayed, and I want the script to go and
> > do something else instead.  If I try..
>
> > goto :%parm:~2% || goto :error
>
> > ..that does not work.
>
> > Any solution?
>
> NT shell scripting, on what O/S: nt3.51, nt4, w2k, w2k3, w2k8, xp?
>
> I don't think you can trap an attempt to go to an invalid label. The
> alternative is to check the value of the parm variable to see if it is a
> valid label. One way:
>
>     do %%L in (label1 label2 label3) if "%parm%" EQU "%%L" goto:%parm%
>
> Another way would be to use FIND or FINDSTR to search the batch file for
> instances of ":%parm%". Trouble is if there was a "label called ":whatsit"
> and the value of parm was "what"...
>
> /Al

Your batch skills are a bit rusty, Al.  That should be ...

FOR %%L in (label1 label2 label3) DO if "%parm%" EQU "%%L" goto:%parm
%

Tom Lavedas
**************
Author
13 Jun 2009 10:34 PM
Al Dunbar
Show quote Hide quote
"T Lavedas" <tglba***@cox.net> wrote in message
news:a34a7562-8512-4d0f-bf96-bc0b5e4df860@y6g2000prf.googlegroups.com...
> On Jun 12, 11:46 pm, "Al Dunbar" <aland***@hotmail.com> wrote:
>> "Synapse Syndrome [KGB]" <syna...@NOSPAMsyndrome.me.uk> wrote in
>> messagenews:eG0tC$86JHA.1***@TK2MSFTNGP03.phx.gbl...
>>
>>
>>
>> > (NT shell scripting)
>>
>> > Say I have a goto parameter command -
>>
>> > goto :%parm%
>>
>> > If the batch label does not exist, the script ends with the error:
>>
>> > "The system cannot find the batch label specified - whatever"
>>
>> > I do not want the error message displayed, and I want the script to go
>> > and
>> > do something else instead.  If I try..
>>
>> > goto :%parm:~2% || goto :error
>>
>> > ..that does not work.
>>
>> > Any solution?
>>
>> NT shell scripting, on what O/S: nt3.51, nt4, w2k, w2k3, w2k8, xp?
>>
>> I don't think you can trap an attempt to go to an invalid label. The
>> alternative is to check the value of the parm variable to see if it is a
>> valid label. One way:
>>
>>     do %%L in (label1 label2 label3) if "%parm%" EQU "%%L" goto:%parm%
>>
>> Another way would be to use FIND or FINDSTR to search the batch file for
>> instances of ":%parm%". Trouble is if there was a "label called
>> ":whatsit"
>> and the value of parm was "what"...
>>
>> /Al
>
> Your batch skills are a bit rusty, Al.  That should be ...
>
> FOR %%L in (label1 label2 label3) DO if "%parm%" EQU "%%L" goto:%parm
> %
>
> Tom Lavedas
> **************

Yeah, I always forget the DO. Fortunately, that throws an error which
reminds me. So thanks for being the error message in this case ;-)

/Al
Author
13 Jun 2009 8:24 PM
Pegasus [MVP]
Show quote Hide quote
"Synapse Syndrome [KGB]" <synapse@NOSPAMsyndrome.me.uk> wrote in message
news:eG0tC$86JHA.1196@TK2MSFTNGP03.phx.gbl...
> (NT shell scripting)
>
> Say I have a goto parameter command -
>
> goto :%parm%
>
> If the batch label does not exist, the script ends with the error:
>
> "The system cannot find the batch label specified - whatever"
>
> I do not want the error message displayed, and I want the script to go and
> do something else instead.  If I try..
>
> goto :%parm:~2% || goto :error
>
> ..that does not work.
>
> Any solution?
>
> Cheers
>
> ss.

If you want your batch file to be robust then you must check if the
parameter passed to it is a valid label. Relying on some internal error
capturing process is not good a programming technique. You could do it like
this:

@echo off
set Labels=/one/two/three/twenty/
if not "%1"=="" echo %Labels% | find /i "/%1/" > nul || echo Invalid label

Make sure to surround each label name with a forward slash and not to use
any labels with embedded forward slashes.