- 1 year ago
How to do a string replacement on a variable at the windows command prompt?
Comment
1 / 1
In the Windows command prompt, you can perform string replacement on a variable using the following syntax:
batchset "var=%var:search=replace%"
Here's an example to replace "world" with "everyone" in a variable named greeting
:
batch@echo off
set "greeting=Hello, world!"
echo Original: %greeting%
set "greeting=%greeting:world=everyone%"
echo Modified: %greeting%
Output:
makefileOriginal: Hello, world!
Modified: Hello, everyone!
In the above example, the %var:search=replace%
syntax is used to replace all occurrences of "search" with "replace" in the value of the var
variable. The set
command is used to assign the modified value back to the variable.
Note that string replacement in the Windows command prompt is case-insensitive. If you want to perform a case-sensitive replacement or perform more complex string operations, you may need to use a more advanced scripting language like PowerShell or VBScript.