|| Operator not working with Goto command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

|| Operator not working with Goto command

#1 Post by PaperTronics » 05 Jul 2017 02:22

Hey guys,

I've stumbled across another problem in my program. :cry:

I want to use the goto command to go to the label specified in the chkpoint variable. I also want to make sure that if the chkpoint variable is empty (which will cause an error in the goto command) the goto command redirects to a specific label. But I'm unable to do that with this code:

Code: Select all

Goto :%chkpoint%||Goto :specificlabel


If the chkpoint var isn't empty, the code above works as desired, but if it is empty then it causes an error:

Code: Select all

The system cannot find the batch label specified -


Any help is greatly appreciated!

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: || Operator not working with Goto command

#2 Post by npocmaka_ » 05 Jul 2017 04:05

check this - https://stackoverflow.com/a/23335912/388389 (the jeb's answer)
This behavior is used for heredoc in batch scripts - viewtopic.php?t=6493

Though in this case you have something like (as I understand it the variable does not exist)

Code: Select all

goto :||goto :somewherelese


but it still works.Just the cmd is out of the context of the batch file and some commands will be not working (SHIFT,SETLOCAL/ENDLOCAL,GOTO,CALL ::Label ...).
Check this example:

Code: Select all

@echo off

goto : 2>nul||(
   echo it is working
)

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: || Operator not working with Goto command

#3 Post by PaperTronics » 05 Jul 2017 05:39

Thanks npocmaka,

I read the whole post at the SO link you provided and I figured that if the goto command causes an error then it acts like exit /b so as jeb described using Call instead of Goto in the first command should do the trick. And it did!

Gotta go back to coding now!

Thanks again,
PaperTronics

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

Re: || Operator not working with Goto command

#4 Post by aGerman » 05 Jul 2017 10:23

PaperTronics wrote:if the chkpoint variable is empty
...That is, checkpoint is not defined.

Code: Select all

if defined chkpoint (Goto :%chkpoint%) else Goto :specificlabel

Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: || Operator not working with Goto command

#5 Post by PaperTronics » 05 Jul 2017 21:38

aGerman wrote:if defined chkpoint (Goto :%chkpoint%) else Goto :specificlabel


Your method works too aGerman. And I'm gonna stick to your method instead of the Call method coz' I've heard repetitive call commands are slower than repetitive goto commands

Post Reply