Page 1 of 1

segment of variable

Posted: 17 Mar 2014 04:04
by kadkam
Hi

I need to extrapolate a segment of variabile.
Example
C:\YEAR_2014_OK\
I need only "2014"

Actually I know only for remove the right of variable

Code: Select all

@echo off
set pippo="c:\year_2012_OK"
echo %pippo%
echo %pippo:~0,-4%
pause



Can you correct this code to match the example above?
thanks

Re: segment of variable

Posted: 17 Mar 2014 04:42
by Squashman
Your starting offset and length should be 8,4

Re: segment of variable

Posted: 17 Mar 2014 05:47
by kadkam
ops ...was very simple

Code: Select all

echo %pippo:~9,-4%


thanks

Re: segment of variable

Posted: 17 Mar 2014 05:53
by foxidrive
kadkam wrote:ops ...was very simple

Code: Select all

echo %pippo:~9,-4%




Squashman is right when you are going from the left end of the string and it should be 8,4

It skips 8 characters and then takes the next 4 characters but the way you set your sting also matters.

This sets the string without embedded double quotes.

Code: Select all

@echo off
set "pippo=c:\year_2012_OK"
echo "%pippo%"
echo "%pippo:~8,4%"
pause

Re: segment of variable

Posted: 17 Mar 2014 07:47
by Squashman
kadkam wrote:ops ...was very simple

Code: Select all

echo %pippo:~9,-4%


thanks

You do not need to use the minus symbol. You want the first 4 characters starting at the position you need. If the end of your path ever gets longer then you would not get the correct output.

Re: segment of variable

Posted: 17 Mar 2014 08:55
by kadkam
yes, perfect solution is
~8,4%

(With minus is number of character from right to left)