You are not logged in.

#1 02 Mar 2016 15:25

JoseKreif
New Member
Registered: 02 Mar 2016
Posts: 2

The dreadful and hated "GOTO"

--------
mad  TL;DR mad

Is the use of GOTO okay for .cmd scripts?
----------

I'm a little new to CMD scripting. I'm more of a Linux Bash Script kind of guy.

I'm looking at this bat that was written in my company a long time ago. It's pretty neat. It actually echos out lines to create additional .bat and .vbs files at run time.

One thing I notice though, and maybe I dont know enough yet, but it seems to be structured on Labels and GOTOs.

I've always read on the internet of how everyone disrespects GOTOs and even coworkers has advised against using them.

Is using them in the .cmd/.bat scripts ok? If not, what's the proper way? Because it seems the flexibility of .cmd is a little limited.

Offline

#2 02 Mar 2016 16:19

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Re: The dreadful and hated "GOTO"

In my limited experience, labels and goto work well, though some call a structured like this batch "spaghetti" due to jumps to various places making it harder to read and amend by a different coder. But if you write the same batch by separating messages into a common pull for easy translation, and broadly using functions and substitutions, your batch file would be even harder to follow, but more efficient to execute and easier to amend the existing structure with new functionality. So its IMHO a matter of personal choice and also the project size. Small batches may be straightforward, no need to separate and reuse repeatable code more efficiently.

Offline

#3 02 Mar 2016 18:16

guizalan
Member
Registered: 22 Feb 2016
Posts: 2

Re: The dreadful and hated "GOTO"

I use them, when it's something simple, never for loops or complex stuff. One example, is when validating a choice, go back and ask again.

:START
set /P choice=Do you wish to continue[Y/N]?
if /I '%Choice%'=='Y' goto COPYFILES
if /I '%Choice%'=='N' goto QUIT
echo '%Choice%' is not a valid choice
GOTO START

:COPYFILES
bla bla bla
:QUIT
ECHO goodbye

Offline

#4 03 Mar 2016 00:50

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: The dreadful and hated "GOTO"

The language was invented before the "never use goto" recommendation came about. While you can avoid using goto the vast majority of the time, there's nothing wrong with using it in batch.

Offline

#5 03 Mar 2016 14:45

JoseKreif
New Member
Registered: 02 Mar 2016
Posts: 2

Re: The dreadful and hated "GOTO"

Thanks guys.

I just want to know what the options are if I decide to pickup batch to possibly do more for my company.

Offline

#6 03 Mar 2016 16:41

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: The dreadful and hated "GOTO"

They have their place.  I tend to prefer "call :label", as this a more (though not entirely) analogous to functions in bash.

Last edited by bluesxman (03 Mar 2016 16:45)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by