Discussion:
Please Explain: set choice=%choice:~0,1% (sample bat file included)
(too old to reply)
Keith
2004-10-17 10:38:02 UTC
Permalink
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
Matthias Tacke
2004-10-17 12:02:53 UTC
Permalink
Post by Keith
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?
It isn't easy to guess your experience with batches.

Please don't start a new thread on the same topic.

This batch will only run under an nt line os, so the proper group for
this question is news:alt.msdos.batch.nt

If you don't have an nt line os, it won't work on your pc.

There was the advice in my previous answer, see:
set /?
(please in a cmd window)

<snip>
Post by Keith
if not '%choice%'=='' set choice=%choice:~0,1%
If not empty, replace choice with choice cut after the first character.
Input 12 for example and you'll see that input is taken as 1.
Post by Keith
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?
This is useful in cases where a possible answer is yes/no or y/n -
stripped to only one char it eases the comparison with also "if /I" to
ignore case.

HTH
--
Greetings
Matthias
foxidrive
2004-10-17 12:45:58 UTC
Permalink
Post by Keith
I asked this question yesterday
It seems that in these days batch file techniques have become part of
various student courses. I don't know how others feel but I am averse to
providing an answer that can simply be copy and pasted into an
assignment...

Your question yesterday seemed to me to be course related, but it's
difficult to be sure as some recent and very creatively posed queries also
seem to me to be course related.

Back in my day when I plagiarised an assignment I had to at least copy the
work in my own handwriting. :)

Loading...