segment of variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

segment of variable

#1 Post by kadkam » 17 Mar 2014 04:04

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: segment of variable

#2 Post by Squashman » 17 Mar 2014 04:42

Your starting offset and length should be 8,4

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: segment of variable

#3 Post by kadkam » 17 Mar 2014 05:47

ops ...was very simple

Code: Select all

echo %pippo:~9,-4%


thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: segment of variable

#4 Post by foxidrive » 17 Mar 2014 05:53

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: segment of variable

#5 Post by Squashman » 17 Mar 2014 07:47

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.

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: segment of variable

#6 Post by kadkam » 17 Mar 2014 08:55

yes, perfect solution is
~8,4%

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

Post Reply