SETREPLACE - Replace a substring with another substring |
Top Previous Next |
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 |