views:

40

answers:

3

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?

+1  A: 

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

mafutrct
sorry for the lolcat reference but i just could not resist >_<
mafutrct
LOL it's coool.
Haoest
+1  A: 

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)

Philip Rieck
omg! should have posted question before I put so much time into it. Thank you!
Haoest
A: 

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
Metro Smurf