0
cls
@echo off
color 1F
echo.
set "scelta="
set /p scelta=Scelta:
set "A="
set /a "A=%scelta%"
echo Scelta: %scelta%
echo A: %A%
if "%A%"=="1" (
   echo Valore 1
@pause
) else if "%A%"=="0" (
       echo Valore 0
@pause
)
goto:Scelta

When I enter, 1, it displays 1. Is OK.

When I enter, 0, it displays 0. Is OK.

When I enter, \, it displays 0. Why?

When I enter, "\, it displays 0. Why?

Compo
  • 36,585
  • 5
  • 27
  • 39
Erminio
  • 1
  • 1
  • The first thing you should understand is that `Set /A` doesn't magically convert a variable's data from a string to an integer, so there's no need to use `Set /A` unless it's to modify their entry. It is even more important to understand that an end user can enter absolutely anything at the `Set /P` prompt, which means that you must ensure that only permitted input is accepted and used. You have not done that, which I'd say is especially important given that a prompt of just `Scelta:`, provides no indication to the end user of what to enter. Could you please explain what is acceptable input? – Compo Dec 19 '20 at 19:34
  • 1
    `set /a` tries its best to turn a string into a mathematical expression and evaluate it. Invalid substrings may be interpreted as a (probably empty) variable. Note that `set /a var=variable+2` adds two to a variable `variable`. `set /a` doesn't need the percent signs around a variable name. – Stephan Dec 19 '20 at 22:07
  • @Erminio, without wishing to sound rude, there are multiple answers, not just one. People have given their time in order to assist you with your problem, the least you could do is to try their code, and provide appropriate feedback to each of them as separate comments. My existing answer, uses the appropriate command, and has been updated to cater for entries from `0` to `7` inclusive, _(as opposed to just `0`, and `1` as implied in your posted code)_. – Compo Dec 20 '20 at 12:00

3 Answers3

0

When you use set /a to set a variable and you provide something other than a number, the variable gets set to 0.

set /a "A=%scelta%"

will set %A% to 0 when you give it \ or "\ because those are not numbers. Since %A% is now 0 according to the script, the two if statements at the bottom get interpreted by the script like this:

if "0"=="1" (
   echo Valore 1
@pause
) else if "0"=="0" (
       echo Valore 0
@pause
)

As you can see, 0 equals 0, so Valore 0 gets displayed.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Thanks for the answers. We do it differently. Unfortunately I have a low knowledge of the subject. Do you recommend which command lines are best to use? The choice is from 0 to 7. Only these values must be accepted. All other values, including " or "\ must not be accepted. For example, by chance I tried to insert 1" and I can't exclude it. If I insert the line if "%scelta%"=="1"" echo NO it doesn't work. As soon as I enter 1 "it exits the batch file. Thanks. – Erminio Dec 20 '20 at 10:17
  • 2
    @Erminio: `The choice is from 0 to 7. Only these values must be accepted. All other values, including " or "\ must not be accepted.`: the [choice](https://ss64.com/nt/choice.html) command does exactly that. Is there a specific reason you refuse to use it? – Stephan Dec 20 '20 at 12:12
0

set /a calculates the value of the argument, seen as a number of terms separated by operators or spaces.

A term may be numeric (and a leading zero directs the term to be Octal) or a variable-name.

If a variable name is defined and has a numeric value, that value is used, otherwise 0 is used.

The resultant expression is then syntax-checked for validity. If it is valid, the result of the expression is assigned, otherwise an error is raised.

In your case, \ and "\ are probably seen as variables which are not assigned - in fact, if you set "\=51" then entering \ will use 51 in the calculation.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks for the answers. I used Magoo's advice, set set "\ = 8" and it worked. I have one last problem when I enter the value 0" I can't identify it. I tried to use the line if "%scelta%" NEQ "0" goto start, but it doesn't work. How can I identify the value 0 "? Thanks. – Erminio Dec 20 '20 at 19:13
  • `NEQ` stands for **N**ot **EQ**ual. Is that what you want? – Stephan Dec 20 '20 at 21:58
  • I want when I enter 0" the file does not close. – Erminio Dec 20 '20 at 22:45
  • why don't you use `choice`, as suggested several times here and on your previous question? It solves all your problems. – Stephan Dec 21 '20 at 10:11
  • Someone explain to me why it is not possible to identify the value 1" or 0" ? Thanks. – Erminio Dec 24 '20 at 18:17
  • I'm having difficulties understanding what you are typing in response to your `Scelta:` prompt. Are you typing `1` or `0` or `1"` or `"1` ? enclosing a literal string between backtick (`\``) characters should show the string highlighted. I'm baffled by why you would want to input any string containing `"` as it introduces processing problems with the language syntax - and it's not the only character that causes difficulties, some of which can be overcome and others really can't. – Magoo Dec 24 '20 at 18:38
  • I explained why it doesn't work and how to make it work as an answer to your (meanwhile deleted) other question. What was wrong with it? Besides, you still didn't tell us why you are refusing to use `choice`? – Stephan Dec 24 '20 at 19:17
  • Regards, I solved. Thank you very much for your help. Happy Holidays. – Erminio Dec 26 '20 at 16:36
0

Your current code suggests that only entries of 0 and 1 are permitted. According to your comment however, your intention is to receive inputs of only 0, 1, 2, 3, 4, 5, 6, and 7. The easiest way to do that is to use the correct command; the choice command!

The choice command, (technically the built-in choice.exe utility), benefits in that you can only submit entries which match those listed at its /C option.

@Echo Off
SetLocal EnableExtensions
Color 1F
ClS
Echo(
%__AppDir__%choice.exe /C 01234567
Set /A A = %ErrorLevel% - 1
Echo Valore %A%
Pause

To learn how to use the choice command, open a Command Prompt window, type choice /?, and press the ENTER key.

From the usage information you could then, if you still wanted to not give your end user an indication of what their possible choices, (sceltas), were, you could match your initial prompt:

%__AppDir__%choice.exe /C 01 /N /M "Scelta:"
Compo
  • 36,585
  • 5
  • 27
  • 39