Page 1 of 1

|| Operator not working with Goto command

Posted: 05 Jul 2017 02:22
by PaperTronics
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!

Re: || Operator not working with Goto command

Posted: 05 Jul 2017 04:05
by npocmaka_
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
)

Re: || Operator not working with Goto command

Posted: 05 Jul 2017 05:39
by PaperTronics
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

Re: || Operator not working with Goto command

Posted: 05 Jul 2017 10:23
by aGerman
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

Re: || Operator not working with Goto command

Posted: 05 Jul 2017 21:38
by PaperTronics
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