简体   繁体   中英

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" 

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM