Home > Shell Scripting, Windows > Manipulating the values of environment variables in windows batch files

Manipulating the values of environment variables in windows batch files


Lets say you have an environment variable that contains a certain value and you want to change it in some manner.
Example:

set AAA=abc_123
set BBB=%AAA:~0,3%%AAA:~4,3%
echo %BBB%

The output will be abc123
The syntax is: %var:~<offset>,<count>%
Examples:
echo %AAA:~0,1% – will print “a” – Start at offset 0 and count 1 character.
echo %AAA:~1,2% – will print “bc” – Start at offset 1 and count 2 characters.
echo %AAA:~3,3% – will print “_12” – Start at offset 3 and count 3 characters.
echo %AAA:~1,-2% – will print “bc_1” – Start at offset 1 and count 2 characters from the end.

Another option is

set AAA=abc_123
set BBB=%AAA:abc=ABC%
echo %BBB%

The output will be ABC_123
The syntax is: %var:<string to find>=<alternate value>%
Examples:
echo %AAA:a=A% – will print “Abc_123”.
echo %AAA:_=-% – will print “abc-123”

There are more options – just run help set on the command line to find them out.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: