Home Windows XP XP Syntax

Syntax : Variable Editing

Use the syntax below to edit individual characters in a string variable.

Syntax
      %variable:StrToFind=NewStr%

      %~[param_ext]$variable:Param

Key
   StrToFind    : The characters we are looking for
   NewStr       : The chars to replace with (if any)
   variable     : The environment variable
   param_ext    : Any filename Parameter Extension
   Param        : A command line parameter (e.g. 1)

This syntax can be used anywhere that you would otherwise refer to the whole %variable% such as ECHOing the variable to screen, setting one variable = another

"StrToFind" can begin with an asterisk, in which case it will replace all characters to the left of "StrToFind".
By leaving NewStr blank, you can delete characters.

Using both an asterisk and NewStr=null effectively provides a left$() or right$() function.

Examples:
The variable _test containing 123456789abcdef0 is used for all the following examples:

::Edit and replace the character string 'ab' with 'xy'
   SET _test=123456789abcdef0
   SET _result=%_test:ab=xy%
   ECHO %_result%          =123456789xycdef0

::Delete the character string 'ab'
   SET _test=123456789abcdef0
   SET _result=%_test:ab=%
   ECHO %_result%          =123456789cdef0

::Delete the character string 'ab' and everything before it
   SET _test=123456789abcdef0
   SET _result=%_test:*ab=% 
   ECHO %_result%          =cdef0

::Replace the character string 'ab' and everything before it with 'XY'
   SET _test=123456789abcdef0
   SET _result=%_test:*ab=XY% 
   ECHO %_result%          =XYcdef0


:: To remove characters from the right hand side of a string is 
:: a two step process and requires the use of a CALL statement
:: e.g.

   SET _test=The quick brown fox jumps over the lazy dog

   :: To delete the string 'brown' and everything after it 
   :: First delete everything before 
   SET _endbit=%_test:*brown=%
   Echo We dont want: [%endbit%]
::Now remove this from the original string
   CALL SET _result=%%_test:%_endbit%=%%
   echo %_result% 

More Examples:

The variable %_cities% contains
"Aberdeen, London, Edinburgh"

To change "London" into "Hammersmith" use this command:

SET _cities=%_cities:London=Hammersmith%

This will result in
"Aberdeen, Hammersmith, Edinburgh"

Removing words from a text string

To effectively delete ALL occurrences of "StrToFind" from your output, set "NewStr" to an empty string.

e.g. the variable %_cities% contains
"Aberdeen, London, Edinburgh"

To remove the string "London" we would use:

SET _cities=%_cities:London=%

Removing spaces from a text string

To remove all spaces from %_cities% we would use:

SET _cities=%_cities: =%

This will result in
"Aberdeen,London,Edinburgh"

Boolean Test "does string exist ?"

To test for the existence of a value we can replace the value with itself, placing the result in a second variable and then testing that variable with EQU

"StrToFind" can begin with an asterisk, in which case it will replace all characters to the left of "StrToFind".

If we have a variable %_cities% containing text (that could be in any order)
"Aberdeen, London, Edinburgh"

To test for the existence of the string "London"

SET _marker=%_cities:*London=London%
:: The text London is 6 chars long so remove any extra
SET _marker=%_marker:~0,6%
IF %_marker% EQU London ECHO London was found

Finding items within the PATH environment variable

The %PATH% variable contains a list of folder names.

If you have a parameter containing a valid 'folder' this can be compared with the PATH variable.

This is done using the syntax:

$variable:paramater

For example
If the current %PATH% =
C:\WINNT\system32;C:\WINNT;C:\utils\jdk\bin

and the batch has a parameter %1 =
C:\utils\jdk\bin

To get the drive and Path
ECHO %~dp$PATH:1
This will either return "C:\utils\jdk\bin" or a NULL if the item is not found in the current %PATH%

If the value was supplied as %2 then the syntax becomes:

ECHO %~dp$PATH:2

This syntax can be applied where:

Be wary of using the syntax on this page to modify the PATH - the User path can be edited, but the System path remains read-only for most users.

Related Commands:

SUBSTRING of a variable :~

PARAMETERS - Filename Parameter Extensions

PATHMAN - This Resource Kit utility allows quick modification of both the system and user paths. Pathman can resolve many problems such as duplicate characters, and can improve performance by removing duplicate paths. For details see Pathman.wri in the resource kit.



Back to the Top

Simon Sheppard
SS64.com