-2

I am trying to create a batch file which will create some directories, and add some environment variables depending on the user response. However, I cannot get the condition working according to the input:

I just extracted the decision code:

@echo off
set /P answer="Do you want to install the Default configuration? (Home folder will be not created, dev_tools will install con C:\) "

if !answer! == "Y"
echo "Setting environment variables on C:\.. and Default user Folder."
else
echo "Setting environment variables on User Folder."
PAUSE

But when I executed that code, I receive the following message on the CMD window:

The syntax of the command is incorrect.

I just need to print one of the two messages inside the IF.

Mofi
  • 42,677
  • 15
  • 72
  • 128
  • 1
    `IF` syntax is `if string1 operator string2 action` If `string1` or `string2` is "quoted" (which allows a string to contain spaces) then **BOTH** strings must be quoted. – Magoo Jun 01 '22 at 17:10
  • Please open a command prompt window, run `if /?` and read the output usage help. I strongly recommend to read also [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) and [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). Then you know how to prompt a user for a choice and how to compare strings correct in a batch file. By the way: The correct syntax for an __IF__ with an __ELSE__ branch can be seen on thousands of answers here. – Mofi Jun 01 '22 at 17:22
  • 2
    ... and when you use delayed expansion, you have to enable it before (`setlocal enabledelayedexpansion`), else an exclamation mark is just an exclamation mark. – Stephan Jun 01 '22 at 17:24
  • 2
    I summarized the most often used four common valid syntax variants using an __IF__ condition with an __ELSE__ branch in [this answer](https://stackoverflow.com/a/34118487/3074564). But the usage of command __CHOICE__ would be better for the choice prompt. – Mofi Jun 01 '22 at 17:25
  • 2
    You can also use `if string1 operator string2 (actiontrue) else (actionfalse)` *ON ONE PHYSICAL LINE* `actiontrue` *must* be parenthesised and `actionfalse` *may* be parenthesised. A new line may occur directly after either `(` or directly before either `)` – Magoo Jun 01 '22 at 17:47
  • Hi Thanks for helping me with i do a reasearch as you explain if /? and i was able to correct the If syntax. @Mofi. Here is my new working code:@echo off set /P answer="Do you want to install the Default configuration? Home folder will be not created, dev_tools will install on C:)" if /i %answer% == Y ( echo Setting environment variables on C:\.. and Default user Folder. ) else ( echo Setting environment variables on %USERPROFILE%\Dev_Tools\ ) PAUSE – AdolfoRM Jun 01 '22 at 18:45

0 Answers0