Keith
2004-10-17 10:38:02 UTC
I asked this question yesterday and received an answer of sorts - but
it does not make sense to me at all. The user that responded may be
right in what he is saying but he does not say why the line is there?
He says it means "Return from offset zero"? "Begin one character"?
What does that mean?
Here is the sample code. A simple SET command with 3 possible
choices. The variable "choice" probably should have been changed
because it confuses the issue with the old choice.com command.
Anyway, I am stymied by the line:
if not '%choice%'=='' set choice=%choice:~0,1%
I have tried the batch file without that line and it works fine, and
it takes care of all situations, such as the user does not make a
proper choice, or hits Enter without making a choice - it simply asks
again. But that weird command - what on earth is it for?
@echo off
REM
---------------------------------------------------------------------------------
REM | sample batch file using the SET command
|
REM | at the bottom where the echo commands tell the user what
choice they made |
REM | you can add anything you want, including multiple commands
|
REM
----------------------------------------------------------------------------------
cls
:start
echo.
echo. You have 3 choices:
echo.
echo 1. enter option 1 here
echo 2. enter option 2 here
echo 3. enter option 3 here
REM Clear the Environment variable, "choice"
set choice=
REM Tell user to enter choice, and assign it to the "choice" Variable
set /p choice=Enter your choice now :
REM strip all but the first char from the content of var choice.
REM the :~0,1 means return from offset zero (aka begin one character)
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto CHOICE1
if '%choice%'=='2' goto CHOICE2
if '%choice%'=='3' goto CHOICE3
echo "%choice%" is not valid please try again
echo.
goto start
REM act on the choice that the User made
REM replace the "echo" lines with whatever you want to do
:CHOICE1
echo You entered 1
goto end
:CHOICE2
echo You entered 2
goto end
:CHOICE3
echo You entered 3
:end
it does not make sense to me at all. The user that responded may be
right in what he is saying but he does not say why the line is there?
He says it means "Return from offset zero"? "Begin one character"?
What does that mean?
Here is the sample code. A simple SET command with 3 possible
choices. The variable "choice" probably should have been changed
because it confuses the issue with the old choice.com command.
Anyway, I am stymied by the line:
if not '%choice%'=='' set choice=%choice:~0,1%
I have tried the batch file without that line and it works fine, and
it takes care of all situations, such as the user does not make a
proper choice, or hits Enter without making a choice - it simply asks
again. But that weird command - what on earth is it for?
@echo off
REM
---------------------------------------------------------------------------------
REM | sample batch file using the SET command
|
REM | at the bottom where the echo commands tell the user what
choice they made |
REM | you can add anything you want, including multiple commands
|
REM
----------------------------------------------------------------------------------
cls
:start
echo.
echo. You have 3 choices:
echo.
echo 1. enter option 1 here
echo 2. enter option 2 here
echo 3. enter option 3 here
REM Clear the Environment variable, "choice"
set choice=
REM Tell user to enter choice, and assign it to the "choice" Variable
set /p choice=Enter your choice now :
REM strip all but the first char from the content of var choice.
REM the :~0,1 means return from offset zero (aka begin one character)
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto CHOICE1
if '%choice%'=='2' goto CHOICE2
if '%choice%'=='3' goto CHOICE3
echo "%choice%" is not valid please try again
echo.
goto start
REM act on the choice that the User made
REM replace the "echo" lines with whatever you want to do
:CHOICE1
echo You entered 1
goto end
:CHOICE2
echo You entered 2
goto end
:CHOICE3
echo You entered 3
:end