My batch file exits upon reaching the nested IF statement

I just tried to code a simple batch which will help me copy some files with some user-slected options. This batch file exits as soon as it reaches the big if statement

Rem @echo off SETLOCAL ENABLEDELAYEDEXPANSION set /a verifier="true to ok: " :begin echo Welcome to install! echo Y - Install echo N - Remove choice /m "Select Y/N : " if "!errorlevel!"=="1" ( echo Installation: echo Y - Default echo N - Custom choice /m "Select Y/N : " if "!errorlevel!"=="2" ( echo Custom selected goto custom ) else ( xcopy %cd% C:\FPC\ if "%verifier%"=="true" ( echo Install OK! ) else ( echo Installation goes wrong! ) ) echo echo Y - Run now echo N - Don't run choice /m "Select Y/N :" if "!errorlevel!"=="1" ( echo Run start start.cmd ) else ( echo Don't run ) goto theend ) else ( echo Removal goto remove ) SETLOCAL DISABLEDELAYEDEXPANSION goto theend :remove echo delete something goto theend :custom echo run another batch file goto theend :theend echo the end pause 

The batch exits immediately after I typed the (Y/N) choice. I guess the problem comes from the big if, but I can't find where is wrong or what I'm missing here. Thank you for your help! Sorry for my bad english.

8

1 Answer

Your use of Choice is somewhat... mangled. And your Nested If's are an Unnecesary Complication - Your Making things Harder on yourself than necessary.

Syntax should be: CHOICE /N /C:ir /M "[I]nstall [R]emove" IF ERRORLEVEL ==2 GOTO Remove IF ERRORLEVEL ==1 GOTO Install :Install CHOICE /N /C:dc /M "[D]efault [C]ustom" IF ERRORLEVEL ==2 GOTO Custom IF ERRORLEVEL ==1 GOTO Default :Default xcopy %cd% C:\FPC\ || GOTO Install_Error CHOICE /N /C:re /M "[R]un [E]xit" IF ERRORLEVEL ==2 GOTO Run IF ERRORLEVEL ==1 GOTO theend :Remove ::: options /actions :Custom ::: options /actions :Run ::: options /actions :Install_Error ::: options /actions :theend ::: options /actions 

Note: || At the end of xcopy Does the following Command when the command preceding || returns an Errorlevel

Using choice like this saves alot of headaches and makes your code more readable and easier to maintain.

If you want to save yourself some typing, heres a way to make it simpler.

At the start of your batch:

Set "ask=CHOICE /N /C:" Set "doIF=IF ERRORLEVEL ==" 

And then whenever you need choice:

%ask%ir /M "[I]nstall [R]emove" %doIF%2 GOTO Remove %doIF%1 GOTO Install 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like