user1229895
user1229895

Reputation: 2319

BAT :~ syntax for strings

I see the ':~' symbols used pretty often in BAT files, I presume it gets a character at a position, but I couldn't find any confirmation of this, nor on how/when to use it (it appears it can get a range as well).

Please if you could shed some light on this, thanks.

SET STRING=C:\MyDocuments\
IF "%STRING:~-1%"=="\" SET STRING=%STRING:~0,-1%
ECHO String: %STRING%

Also should %%A:~-1 work? or would I need to enclose it in ""?

Upvotes: 0

Views: 1158

Answers (1)

user330315
user330315

Reputation:

Type

help set 

in the commandline and you'll get a full description. Some is also documented in the help of the for command. So help for will give you additional information.

This is part of the output of the "help set" command:

May also specify substrings for an expansion.

    %PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.  If the length is not specified, then it defaults to the
remainder of the variable value.  If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

    %PATH:~-10%

would extract the last 10 characters of the PATH variable.

    %PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

Upvotes: 1

Related Questions