Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

13 Experts available now in Live!

Get 1:1 Help Now