Solved

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

Posted on 2007-12-04
3
294 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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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