Chris
Chris

Reputation: 1283

How do you include a goto label in a command line parameters for cmd.exe ?

Does anyone know how can you include goto label in the command string passed to cmd.exe ?

Something like this:

cmd.exe /c ":retry & copy /y new_file.exe file.exe & if [%errorlevel%]==[1] goto :retry" 

Upvotes: 3

Views: 2193

Answers (2)

dbenham
dbenham

Reputation: 130819

You could accomplish your logic by putting your command in an infinite loop, and have it EXIT upon success. I'm not sure I recommend this, since an error could result in a truly infinite loop.

cmd.exe /c "for /l %N in () do @copy /y new_file.exe file.exe && exit"

I suppose you could add a counter that EXITs after N number of failed attempts. Here is an example that exits immediately upon success, and also exits if it fails 3 times.

cmd.exe /c "for /l %N in () do @copy /y new_file.exe file.exe && exit || >nul 2>&1 set /a 1/((failCnt+=1)%3) || exit"

Upvotes: 6

jeb
jeb

Reputation: 82217

It's simple, you can't use goto or call with a label on the cmd line.
Labels work only inside batch files.

But if your line is itself in a batch file, you could restart your batch file with a parameter.

mybatch.bat

@echo off
if "%1"=="intern" goto %2

echo First start
cmd.exe /c myBatch.bat intern :myLabel
echo after
exit /b

:myLabel
echo second start
exit /b

Upvotes: 3

Related Questions