Wow, never thought I would ever write anything in DOS. Now that I do, I know why I never wanted to. The syntax is absurd!
Anyways I need help please. I would like to prompt the user for input, and if a blank line is received, I would like to use the default value, like this:
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 says "set was unexpected at this time."
Can you help please?
[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.
[ %% ] (Percent Percent, or Double Percent) is employed in the "FOR" command when it is used within a batch file. It represents the variable used in that command. It is followed by a single letter, although as stated above, some newer DOS versions allow multiple letters.
String comparison by Using String Library Functionstrcmp() function is used to compare two strings. The strcmp() function takes two strings as input and returns an integer result that can be zero, positive, or negative. The strcmp() function compares both strings characters.
Try
set name=abraham
set /p name=please enter your name, press enter to use %name%:
echo entered : %name%
Note that in cmd files, if nothing is entered, the var is not changed.
Or, with the 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%
Note the quotes around input in the if statement, and notice that I am clearing out input before running (or it will hold the last value if nothing is entered by the user)
Empty strings are actually empty in shell programming, so try if "%input%"=="" set...
(with quotes) or if %input%== set...
(empty string is empty).
I believe you need to put single quotes (not sure if double or single matter) 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With