Home NT NT 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".

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

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

::Delete the character string 'ab'
   SET v_test=123456789abcdef0
   SET v_result=%v_test:ab=%
   ECHO %v_result%          =123456789cdef0

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

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

More Examples:

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

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

SET v_cities=%v_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 %v_cities% contains
"Aberdeen, London, Edinburgh"

To remove the string "London" we would use:

SET v_cities=%v_cities:London=%

Removing spaces from a text string

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

SET v_cities=%v_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 %v_cities% containing text (that could be in any order)
"Aberdeen, London, Edinburgh"

To test for the existence of the string "London"

SET v_marker=%v_cities:*London=London%
:: The text London is 6 chars long so remove any extra
SET v_marker=%v_marker:~0,6%
IF %v_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, remember that though the User path can be edited, 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.



Simon Sheppard
SS64.com