How to fulfill the string comparison condition in DOS?

Wow, I never thought that I would ever write anything in DOS. Now that I do this, I know why I never wanted this. The syntax is absurd!

In any case, I need help, please. I would like to ask the user for input, and if an empty string is received, I would like to use the default value, for example:

set name=abraham. set /p input=please enter your name, press enter to use %name%: if not %input%=="" set name=%input% echo your name is %name% 

I get an error: "set was unexpected at this time."

You can help?

+4
source share
3 answers

Try

 set name=abraham set /p name=please enter your name, press enter to use %name%: echo entered : %name% 

Please note that in cmd files, if nothing is entered, var does not change.

Or with if:

 set name=abraham set input= set /p input=please enter your name, press enter to use %name%: if "%input%" NEQ "" set name=%input% echo entered : %name% 

Pay attention to the quotes around the input in the if statement and note that I clear the input before starting (or it will hold the last value if the user does not enter anything)

+8
source

Empty lines are actually empty in shell programming, so try if "%input%"=="" set... (with quotes) or if %input%== set... (empty line is empty).

+2
source

I believe that you need to put single quotes (not sure if double or single substance) around the variable:

 @echo off set name=abraham. set /p input=please enter your name, press enter to use %name%: if not '%input%'=='' set name=%input% echo your name is %name% pause 
+1
source

All Articles