4

I am struggling with a simple check if the user input was just empty (Enter) and in case set the variable to a new value... spend an hour but it does not work. Goal is to have the entered Value beginning with a backslash OR just an empty string if nothing was entered.

Code:

SET myTargetServerInstanceName=anyNOPEdummyValue
SET /P myTargetServerInstanceName=Enter Target Server INSTANCE Name:    
IF %myTargetServerInstanceName%==anyNOPEdummyValue SET myTargetServerInstanceName=
IF NOT %myTargetServerInstanceName% == [] SET myTargetServerInstanceName=\%myTargetServerInstanceName%

this results in error:

SET was unexpected at this time.

3 Answers 3

19

The usual way to check for an empty variable is compare if its "%value%" is "". However, in order for this to work you must delete the variable before the set /P, because if the user just press enter the previous value of the variabe is not modified:

set "var="
set /P var=
if "%var%" equ "" set "var=default value"

However, a simpler method is use if defined:

set "var="
set /P var=
if not defined var set "var=default value"

Previous method would not require delayed expansion.

As a corollary of previous description, you may get the same result this way:

set "var=default value"
set /P var=
Sign up to request clarification or add additional context in comments.

1 Comment

This indicated to the right direction. The following is working for me: IF [%myTargetServerInstanceName%] == [] SET myTargetServerInstanceName = \%myTargetServerInstanceName%
3

You can check if the set /p operation was sucessful

set /p "var=prompt text?" || set "var=default value"

If you prefer the set / if, when you check for a value that can be empty or contain spaces, it is better to use quotes

if "%var%"=="" set "var=default value"

or, as a variable without content is not defined, you can check this case with

if not defined var set "var=default value"

1 Comment

Good trick to set default value. But when I test different values, it seems that the quotes are not required even if the values contains spaces! Are you sure the format {set /p var=prompt text:} or set var=default value with space may have errors in some cases? I'm not happy to put quote before the var name (as we always expect put quotes around the value itself not the var)
0

I suggest to enclose both sides of the if comparison parts in ":

SET myTargetServerInstanceName=anyNOPEdummyValue
SET /P myTargetServerInstanceName=Enter Target Server INSTANCE Name:    
IF "%myTargetServerInstanceName%"=="anyNOPEdummyValue" SET myTargetServerInstanceName=
IF NOT "%myTargetServerInstanceName%"=="" SET myTargetServerInstanceName=\%myTargetServerInstanceName%

If one of the comparison expressions (left or right of the ==) expands to an empty string, you will receive a syntax error.

Comments

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.