I have a batch file that's not properly using the goto commands the way I want it to. Here's the batch itself (some comments and extra echo commands cut out to make it fit here better). I'll call this " compile.bat
".
@echo off
set "consoleChoice=%~1"
goto consoleChoiceCheck
REM console not selected, main compiler prompt not used, pick console
:consoleChoicePrompt
CHOICE /C 12345 /M "Which console are you using? "
IF ERRORLEVEL 5 SET consoleChoice=XB & goto section2
IF ERRORLEVEL 4 SET consoleChoice=PSP & goto section2
IF ERRORLEVEL 3 SET consoleChoice=PS2 & goto section2
IF ERRORLEVEL 2 SET consoleChoice=GC & goto section2
IF ERRORLEVEL 1 SET consoleChoice=PC & goto modPackSection
REM Checks if console was selected from main compiler script/if main compiler script was used
:consoleChoiceCheck
if "%consoleChoice%"=="" goto consoleChoicePrompt
:modPackSection
REM get mod pack choice from main compiler script
set "modPackChoice=%~2"
goto modPackChoiceCheck
REM mod pack not selected, main compiler prompt not used, pick mod pack
:modPackChoicePrompt
CHOICE /C 1234 /M "Which Mod Pack are you using? "
IF ERRORLEVEL 4 SET modPackChoice=MUE & goto section2
IF ERRORLEVEL 3 SET modPackChoice=BHE & goto section2
IF ERRORLEVEL 2 SET modPackChoice=AXE & goto section2
IF ERRORLEVEL 1 SET modPackChoice=X2UP & goto section2
REM Checks if mod pack was selected from main compiler script/if main compiler script was used
:modPackChoiceCheck
if "%modPackChoice%"=="" goto modPackChoicePrompt
:section2
...
Here's the relevant code from the batch file that calls this one. I'll call this " globalCompile.bat
"
CHOICE /C 12345 /M "Which console are you using? "
IF ERRORLEVEL 5 SET consoleChoice=XB & goto :skinPackSection
IF ERRORLEVEL 4 SET consoleChoice=PSP & goto :skinPackSection
IF ERRORLEVEL 3 SET consoleChoice=PS2 & goto :skinPackSection
IF ERRORLEVEL 2 SET consoleChoice=GC & goto :skinPackSection
IF ERRORLEVEL 1 SET consoleChoice=PC & goto :modPackSection
CHOICE /C 1234 /M "Which Mod Pack are you using? "
IF ERRORLEVEL 4 SET modPackChoice=MUE & goto skinPackSection
IF ERRORLEVEL 3 SET modPackChoice=BHE & goto skinPackSection
IF ERRORLEVEL 2 SET modPackChoice=AXE & goto skinPackSection
IF ERRORLEVEL 1 SET modPackChoice=X2UP & goto skinPackSection
...
if %consoleChoice%==PC (
call compile.bat %consoleChoice% %modPackChoice%
) else (
call compile.bat %consoleChoice%
)
As you can see, compile.bat
is set up to run by being called from globalCompile.bat
, but it can be run independently as well. Here's the issue:
compile.bat
runs correctly when called by itself. If you choose PC for consoleChoice
, it will put up the prompt for modPackChoice
. If you choose any other thing for consoleChoice, it will skip over that prompt.globalCompile.bat
runs correctly when you choose the PC option in globalCompile.bat
. It will ask for modPackChoice
, and then run through and call all the respective batch files.globalCompile.bat
messes up the execution of compile.bat
if a non-PC option is chosen for consoleChoice
. The modPackChoice
prompt will not show up whenever I'm inputting things into globalCompile.bat
, but once it starts calling the other batch files and gets to compile.bat
, it will pull up compile.bat
's prompt for modPackChoice
I'm confused why this is happening, especially since it works fine on its own. Of all the batch files called by globalCompile.bat
, compile.bat
is the only one that uses a variable conditionally like this. I thought that setting up an if statement to only use the modPackChoice
variable as an input for compile.bat
if the PC option is selected, but that didn't work as you can see. Really, I feel like it should work without that because the goto section2
command should skip over :modPackSection
entirely. Can anyone help explain why this might be working only when called on its own?
If global
calls compile
with a non-pc choice made, then it supplies compile
with only the first parameter.
Hence, compile
will recognise %~1
, ignore the goto
in :consoleChoiceCheck
as consoleChoice
is not empty, and proceed to :modPackSection
modPackChoice
will be set to empty as %2
is not supplied, and therefore :modPackChoiceCheck
will execute goto modPackChoicePrompt
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.