1
@ECHO OFF
SETLOCAL EnableDelayedExpansion


SET /P tool=Please enter the tool name: 

If %tool%=="fw-test.exe" (echo "The tool name is fw-test.exe") else (echo "Unknown tool name") 

If %tool%=="ick-test.exe" (echo "The tool name is ick-test.exe") else (echo "Unknown tool name")

I get a syntax error. How I should write for working properly?

3
  • Remove @echo off and check the command echoes. What do you enter in the set /P prompt causing the syntax error? Commented Dec 12, 2019 at 12:58
  • I enter fw-test.exe Commented Dec 12, 2019 at 14:33
  • Well, this should not cause a syntax error; I'd expect such when you don't enter anything and just press enter... Commented Dec 12, 2019 at 15:00

1 Answer 1

2

You should not do 2 separate if statements, the one should include the other in the else statement, else you will get both results tool name.exe and unknown tool name simply because if matches the first, but not the second:

@echo off
:select
set /p tool=Please enter the tool name: 

If /i "%tool%"=="fw-test.exe" (
    echo "The tool name is fw-test.exe"
) else (
    If /i "%tool%"=="ick-test.exe" (
        echo "The tool name is ick-test.exe"
    ) else (
        echo Unknown Tool please retry
        goto select
    )
)

Note that I am evaluating both sides of == with double quotes. Else you will never get a match. This is wrong: if var=="var" as the one is not quoted where this: if "var"=="var" will match exactly. I Included the /I option as well as it will allow for the name to be typed as FW-TEST.exe as well as mixed case.

if you were planning on only using selected pre-defined tools, simply use choice instead. It will allow the user to only choose one of the 2 tools:

@echo off

echo 1. fw-test.exe
echo 2. ick-test.exe
choice /c 12 /m "Select a tool"
goto tool%errorlevel%
:tool1
Set "tool=fw-test.exe"
goto show
:tool2
Set "tool=ick-test.exe"
goto show
:show
echo you have selected %tool%
pause
Sign up to request clarification or add additional context in comments.

5 Comments

How I can write a function that when I will write in the answer of this "Please enter the tool name:" the unknown tool it will ask me to enter the valid tool name.
Rather then use the choice option as I have shown. There you can specify what you want the user to select and only use valid tool options.
In that option you write tool1 and tool2 but in my code I write if %tool% with if statement, it means that I have only the name tool. ECHO The tool name is: %tool% if "%tool%"=="fw-test.exe" ( set toolpath1=%exbuild1%\SW\test_w32\fw-test.exe set toolpath2=%exbuild2%\SW\test_w32\fw-test.exe
In that example tool1 and tool2 are labels and not the variable. It does not seem as if you need a variable here as you are enforcing specific tools. Do you have a specific list of valid tools you want to present to a user?
Have a look at how I changed the last example to still set a variable and use that variable in the :show label. But I also updated the original code to do what you wanted in the comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.