Computing.Net > Forums > Disk Operating System > Prompting for variables in batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Prompting for variables in batch

Reply to Message Icon

Name: JSMP
Date: May 20, 2003 at 17:31:16 Pacific
OS: WINDOWS ME
CPU/Ram: Pentuim IV 1.4GHz / 256 M
Comment:

How do you prompt the user for a variable then evaluate it?



Sponsored Link
Ads by Google

Response Number 1
Name: Secret_Doom
Date: May 21, 2003 at 17:10:24 Pacific
Reply:

Read my FAQ:

FAQ #06 - Get user input

There's a simpler way to interact with the user, which is using the replaceable parameters. For instance, if the information the user would need to input is "string", the user would invoke the batch file like;

MyBat.bat string

Here's a sample:

@echo off
if not "%1"=="" if not "%1"=="/?" goto start
echo Sintax: %0 parameter
echo.
goto eof
:start
echo You passed %1 as first parameter!
:eof

That batch file will display a help information if no parameter is passed, or if the /? parameter is passed. Otherwise, it will display a message using the passed parameter. Oh, and the %0 represents the batch filename.

However, that method has its limitations, e.g.: a space will break a string into %1 and %2, instead of only %1.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

____________________________________________________________


0

Response Number 2
Name: jgam
Date: May 22, 2003 at 13:46:36 Pacific
Reply:

A simple, but effective way to do what you want to do is to download a DOS version of the SED (UNIX) program and plug it into a DOS script. I found one that works good, Sed_dos(2).exe (I renamed it sed.exe on my local drive), at the following URL: http://www.winnetmag.com/Files/5697/5697.zip

Extract that executable to a directory in your path (I put mine in the root of C:) then try the following script - minus the 'line ends here' comments:

----------
@echo off
cls
echo Let's do some math... [line ends here]
echo Enter a number: [line ends here]
c:\sed -e "s/^/SET INPUT1=/" -e "q" >c:\temp\Input1.bat [line ends here]
CALL c:\temp\Input1.bat [line ends here]
DEL c:\temp\Input1.bat [line ends here]
echo Enter another number: [line ends here]
c:\james\SED\SED -e "s/^/SET INPUT2=/" -e "q" >c:\temp\Input2.bat [line ends here]
CALL c:\temp\Input2.bat [line ends here]
DEL c:\temp\Input2.bat [line ends here]
SET /a Total=%INPUT1% + %INPUT2% [line ends here]
echo Your total is %Total% [line ends here]
IF %INPUT1% LSS %INPUT2% (echo The first number is smaller.) ELSE echo the first number is bigger. [line ends here]
pause
cls
-----
If you want to see all the comparison operators for DOS type the IF help command at the DOS prompt: if /? Hope it helps...


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Prompting for variables in batch

Variables in batch files www.computing.net/answers/dos/variables-in-batch-files/12289.html

Using 'for' cmd in batch file. www.computing.net/answers/dos/using-for-cmd-in-batch-file/13417.html

Input in Batch www.computing.net/answers/dos/input-in-batch/13254.html