How to set a variable equal to user input without Set /P in simple batch script.


I have a batch file where a default variable is set at the top..then the user is presented with an option to change the default.  If they hit "enter" it leaves default, however if they enter text, then it replaces the default with the text.  I can' get the variable to be set for some reason.  I am pretty new to BAT writing so I am sure its a simple syntax thing.  In the code below, I just need the line that says SET FTPHOST = %sel2% to work.  Right now, it doesn't change the vaule of FTPHOST from the default even when text is entered.  Thanks!

@echo off
set CDROM=d:
SET FTPUN=administrator
SET FTPPW=password
SET FTPDIR=directoryname
setlocal enabledelayedexpansion
set /P sel1=Do you wish to change the defaults? [Y, (N)]:
if /I "%sel1%"=="n" goto :_header
if /I "%sel1%"=="y" goto :_sethost
goto :_header
set /P sel2=Enter the new IP Address [%FTPHOST%]:
   if not "%sel2"=="" goto else
     goto :_setrun
   goto endif
     SET FTPHOST  = %sel2%

Open in new window

Who is Participating?
SteveGTRConnect With a Mentor Commented:

SET FTPHOST  = %sel2%



No spaces.

You may want to consider initializing setl1 to N or whatever you want the default to be prior to the first set /P statement.

There doesn't look to be an an immediate need for the enabledelayedexpansion. I'd recommend removing it and moving the setlocal statement prior to the FTP set commands.
set /P sel2=Enter the new IP Address [%FTPHOST%]:
   if not "%sel2%"=="" goto else
     goto :_setrun
   goto endif
     set FTPHOST=%sel2%

You are missing a % in your IF statement after Sel2.
Also remove the spaces in the Else portion around equals sign.
The above section works.
Also, it is unnecessary to put the : before the goto lines:

goto :_header

Could read as:

goto _header
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.