Solved

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

Posted on 2007-12-04
3
293 Views
Last Modified: 2008-02-01
Hello,

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!

ED7
@echo off

set CDROM=d:

SET FTPHOST=192.168.1.2

SET FTPUN=administrator

SET FTPPW=password

SET FTPDIR=directoryname
 

setlocal enabledelayedexpansion
 

:_manualvariables

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
 

:_sethost

set /P sel2=Enter the new IP Address [%FTPHOST%]:

   if not "%sel2"=="" goto else

     goto :_setrun

   goto endif

   :else

     SET FTPHOST  = %sel2%

   :endif

Open in new window

0
Comment
Question by:electricd7
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 20406224
Change:

SET FTPHOST  = %sel2%

To:

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.
0
 
LVL 8

Expert Comment

by:Ubuntop
ID: 20406226
set /P sel2=Enter the new IP Address [%FTPHOST%]:
   if not "%sel2%"=="" goto else
     goto :_setrun
   goto endif
   :else
     set FTPHOST=%sel2%
   :endif

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.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20406246
Also, it is unnecessary to put the : before the goto lines:

goto :_header

Could read as:

goto _header
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now