?
Solved

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

Posted on 2007-12-04
3
Medium Priority
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

752 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