0

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
  • Run it from the command line (not by double clicking in Explorer), then you can at least read any error messages that would bomb the script out. Also, the script won't be happy if your labels that you try to GOTO don't exist. Commented Dec 7, 2019 at 10:47
  • also, what is this supposed to do ? set /a verifier=true? Commented Dec 7, 2019 at 11:05
  • Also debug batch scripts by leaving echo on e.g. REM @echo off and looking at screen output. Commented Dec 7, 2019 at 11:17
  • You seem to have non-matching ( and ) brackets in a number of places, and an ELSE that does not have an IF before it. Commented Dec 7, 2019 at 11:18
  • Note: in batch scripts you don't have Booleans, only strings and unsigned 32-bit integers, so set /a verifier=true will always set verifier to 0. You can do set verifier=true and test with if "%verifier%"=="true" and if not "%verifier%"=="true" Commented Dec 7, 2019 at 19:04

1 Answer 1

0

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
  • Choice is useful as more than just a means of input. It's a forced script break that ensures only the desired actions occur - Worth using heavily for that alone in any program where the user makes numerous choices. The timeout / default Switch in particular is very useful If your making a program that you want to be Semi Autonomous - Set the defaults up to run it through the default commands, and still have the option to skip or adjust actions depending on output. Commented Dec 30, 2019 at 18:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.