NT Syntax : Variable Editing

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



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

The variable v_test is used for all the following examples:

set v_test=

Edit and replace the character string 'ab' with 'xy'

Delete the character string 'ab'

Delete the character string 'ab' and everything before it

Replace the character string 'ab' and everything before it with 'XY'

Some more detailed 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

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:


For example
If the current %PATH% =

and the batch has a parameter %1 =

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