Extract part of a variable

July 25, 2010 at 15:15:31
Specs: Windows XP
Ok so i'm trying to extract one letter from this other variable and then set them to other varibles. But when i try it, it just ends up with saying this:

Letter 1:A
Letter 2:BC
Letter 3:CD
Letter 4:D

Please tell me what I am doing wrong, And your correction. Thanks!

@echo off
set var1=ABCD
echo Letter 1:%var1:~0,1%
echo Letter 2:%var1:~1,2%
echo Letter 3:%var1:~2,3%
echo Letter 4:%var1:~3,4%
echo Press enter to exit.
pause >nul

See More: Extract part of a variable

Report •

July 25, 2010 at 15:30:02
What is it you are trying to accomplish with this?

All you are doing at the moment is echoing part of var1, if you want it to set it, use this:

SET Letter1=%var1:~0,1%
SET Letter2=%var1:~1,2%
SET Letter3=%var1:~2,3%
SET Letter4=%var1:~3,4%

Report •

July 25, 2010 at 15:58:58
I am going to rewrite this to perform a action to output a hex code when a user enters a letter like b into a text file. Then the hex code will be used in Wpe Pro to edit a code in a game. Also i have the whole code wrote and all I need is this to work so could you please help me?

Report •

July 25, 2010 at 16:02:02
See if my code above suits your needs.

Report •

Related Solutions

July 25, 2010 at 16:04:13
Sorry but no it still doesn't work :(

Report •

July 25, 2010 at 16:12:40
SET /p input=""
SET Letter1=%input:~0,1%
SET Letter2=%input:~1,1%
SET Letter3=%input:~2,1%
SET Letter4=%input:~3,1%

etc. (Continue this on for as long as you'd like, following the pattern, and changing it to extract the letters you'd like)

<Your hex conversion code goes here using Letter1, Letter2, Letter3 etc as variables>

ECHO Letter 1: %Letter1%
ECHO Letter 2: %Letter2%
ECHO Letter 3: %Letter3%
ECHO Letter 4: %Letter4%

Report •

July 25, 2010 at 16:20:35
Lol I just found out the same thing but thanks for the reply.
In %var1:~0,1% 0 is the place before the letter and 1 is the length to the right.

My Code:
@echo off
SET Letter1=%var1:~0,1%
SET Letter2=%var1:~1,1%
SET Letter3=%var1:~2,1%
SET Letter4=%var1:~3,1%
echo %Letter1%
echo %Letter2%
echo %Letter3%
echo %Letter4%
echo Done

Report •

Ask Question