'How do i replace a character with another character in batch?
I need to replace all I's in a variable %ps% with 1's and send the output to another variable %ps2%
Solution 1:[1]
as single replacements, just a simple:
set "ps2=%ps:I=1%"
for multiple replacements either specify new lines for each or you could run a for loop, but you'll need to define the search replace items regardless:
@echo off
setlocal enabledelayedexpansion
set "ps=Iab"
set "search_rep="I=1","a=2","b=4""
if not defined ps2 set "ps2=%ps%"
for %%i in (%search_rep%) do set "ps2=!ps2:%%~i!"
echo %ps2%
Take note of the use of delayedepansion in the above example.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gerhard |
