goto in a one-line command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
skkk
Posts: 3
Joined: 20 Mar 2012 06:41

goto in a one-line command

#1 Post by skkk » 20 Mar 2012 06:55

Hi,

I was wondering if there was a way to define a label and use a goto to this one in a one-line command. I can't find a solution to this problem, I made several attempts but nothing seems to work.

Thank you in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: goto in a one-line command

#2 Post by foxidrive » 20 Mar 2012 07:18

Your question doesn't make sense.

You want to define a label, so there must be code after the label, and somehow you want to jump to the routine.
If you can explain what you need to do it may become clearer.

skkk
Posts: 3
Joined: 20 Mar 2012 06:41

Re: goto in a one-line command

#3 Post by skkk » 20 Mar 2012 08:10

I'm just trying to make a while loop in a one-line command, no matter what the loop does.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: goto in a one-line command

#4 Post by foxidrive » 20 Mar 2012 08:17

It takes two lines. One to set the label and one to branch to the label.

:loop
goto :loop

skkk
Posts: 3
Joined: 20 Mar 2012 06:41

Re: goto in a one-line command

#5 Post by skkk » 20 Mar 2012 08:33

thanks but i'm looking for a way to do it as a one-liner from the command line.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: goto in a one-line command

#6 Post by foxidrive » 20 Mar 2012 08:41

The same question arises: what are you trying to do?

miskox
Posts: 668
Joined: 28 Jun 2010 03:46

Re: goto in a one-line command

#7 Post by miskox » 20 Mar 2012 14:59

Maybe this can help:

Code: Select all

for /L %i IN (1,1,1000) do dir&&ping -n 5 -w 1 127.0.0.1>NUL&&cls


In this way I sometimes check the progress of a file copy operation or something (to see if certain file makes a progress - gains in file size).

Saso

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: goto in a one-line command

#8 Post by jeb » 21 Mar 2012 03:14

foxidrive wrote:It takes two lines. One to set the label and one to branch to the label.

foxidrive wrote:You want to define a label, so there must be code after the label, and somehow you want to jump to the routine.


It's possible, but there is another problem

Code: Select all

<:label <nul echo endless loop? & goto :label
echo end

Output wrote:endless loop?
end


The cause is, that a jump or a call to a label will always set the execution position to the line AFTER the label.
So this can be useful with a CALL but not very useful with a GOTO.

The syntax of labels are described at Call a label as a variable
Or at Experiment with call and label

Btw. I'm using this to initialize my batchLibrary

jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: goto in a one-line command

#9 Post by foxidrive » 21 Mar 2012 03:46

jeb wrote:

Code: Select all

<:label <nul echo endless loop? & goto :label
echo end

Output wrote:endless loop?
end




FWIW this produces the same output.

Code: Select all

@echo off
<:label <nul echo endless loop?
echo end
pause

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: goto in a one-line command

#10 Post by jeb » 21 Mar 2012 04:16

Yes obviously, but you can see the difference if you change the GOTO with a CALL statement

Code: Select all

<:label <nul echo endless loop? & CALL :label & echo I'm in a label line
echo end "%0"
exit /b


Output wrote:endless loop?
end ":label"
I'm in a label line
end "test"


jeb

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: goto in a one-line command

#11 Post by foxidrive » 21 Mar 2012 04:35

Yes, I see that jeb.

I really do wonder what the OP is trying to do on the command line though.

My thought is that a for-in-do might do what they want to do, but they aren't speaking.

Post Reply