SETREPLACE - Replace a substring with another substring

Top  Previous  Next

Syntax:

SETREPLACE

[ variable ]  =  [ source ] [ old substring ] [ new substring ]

Arguments:

[ variable ]

Variable to assign; if the variable does not previously exist it is created.

 

[ source ]

Variable or string containing the full original source string.

 

[ old substring ]

Variable or string defining the substring or pattern to be replaced.

 

[ new substring ]

Variable or string defining the replacement value.

Options:

/ignorecase

Ignore character case in source string when scanning for matching substrings.

 

/regex

old substring is treated as a regular expression and new substring as regular expressions replacement pattern.

 

/startswith

old substring is only replaced if it is at the beginning of source. May not be used with /regex

 

 

This script command scans the [ source ] string and replaces all instances of the [ old substring ] with [ new substring ] and saves the result in [ variable ].

 

Consider the following example.

 

SET string = "This is a string."

SETREPLACE mine = string " a " " my "

 

The resulting mine variable contains the value "This is my string.". The original string variable is unchanged.

 

Consider the following example using regular expressions.

 

SET string = "This is a string."

SETREPLACE mine = string "this is a (.*)" "This was a \1" /regex

 

The resulting mine variable contains the value "This was a string.". The original string variable is unchanged.

 

Consider this example that compares what happens when you add the /startswith option:

 

SET string = "Testing Testing Testing"

SETREPLACE result1 = string "Testing" "Replacement"

SETREPLACE result2 = string "Testing" "Replacement" /startswith

 

The variable result1 will contain "Replacement Replacement Replacement" whereas result2 will only have the first word changed and will contain "Replacement Testing Testing".

 

Consider the following example assuming the existence of a file "C:\original\location\file.txt"

 

 GETNEXTFILE "C:\original\location\*.txt"

SETREPLACE newfile = %nextpath "C:\original\location" "\\server\destination" /startswith /ignorecase

MOVE %nextpath newfile

 

moves "C:\original\location\file.txt" to "\\server\location\file.txt"

 

Related command(s):        SETLEFT, SETMID, SETRIGHT, SETLEN, SETEXTRACT, SETSUBSTR, SET, IFREGEX