Help w/ understanding %var:~x,y%

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
adventboi0516
Posts: 2
Joined: 31 Dec 2012 00:14

Help w/ understanding %var:~x,y%

#1 Post by adventboi0516 » 31 Dec 2012 00:22

I've been playing around with a kind of variable (which I have no idea what the name is).
I'm going to use the time system variable as an example.
%time:~0,5%
That shows the first five digits of the variable, but why does 0 represent the first character in the variable? What the heck? (mindblown) Shouldn't it be 1, not 0?
And what does the second digit, the 5 represent? Does it indicate the position of some letter or number in a variable, or is it just the number of characters to display?
I've really never thought about this, but now its just bugging me...

And what does JUST a single digit do? %time:~5%, whats the purpose of that?

Thanks, bros.
._James_.

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

Re: Help w/ understanding %var:~x,y%

#2 Post by foxidrive » 31 Dec 2012 00:34

Code: Select all

@echo off
echo %time%

echo %time:~0,5% - Skip zero from the start of the time environment variable and take the next 5 characters.

echo %time:~5% - Skip 5 characters from the start and return the rest.

echo %time:~-5% - Return 5 characters from the end of the variable.

echo %time:~2,-5% - skip 2 characters from the start and also 5 characters from the end.  It returns a bit from the middle.
pause

adventboi0516
Posts: 2
Joined: 31 Dec 2012 00:14

Re: Help w/ understanding %var:~x,y%

#3 Post by adventboi0516 » 31 Dec 2012 00:49

Ohhhh. So the first number is how many digits to SKIP? huh.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help w/ understanding %var:~x,y%

#4 Post by dbenham » 31 Dec 2012 09:06

That is definitely a good way to think about it. But it is language specific.

Another way to think about it is convention: Pretty much all languages have the concept of substring operations and array referencing. There are generally two conventions:

0 based indexing - The first character in the string (or element of an array) is position 0
1 based indexing - The first character in the string (or element of an array) is position 1

A given language does not need to be consistent. Some languages may use 1 based indexing for string position and 0 based indexing for array indexing.

I can't remember which language (VB perhaps?), but I know there is at least one that allows you to choose whether you want to use 0 or 1 based indexing for arrays.

The important thing is you must learn what the convention is for your particular language, and work with it.


Dave Benham

DigitalSnow
Posts: 20
Joined: 21 Dec 2012 13:36
Location: United States

Re: Help w/ understanding %var:~x,y%

#5 Post by DigitalSnow » 31 Dec 2012 13:53

DosTips has a very helpful page in reference to String Manipulation here: http://www.dostips.com/DtTipsStringManipulation.php

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

Re: Help w/ understanding %var:~x,y%

#6 Post by Squashman » 31 Dec 2012 14:39

My Drill Sergeant told me several times doing push ups that zero was a number and to start over by counting from zero.

Our Print Software uses a Zero Offset but our data programming software does not. So as Dave has explained you have to remember which programming language or software in my case that you are using to remember how to reference your fixed data.

Post Reply