Articles

Batch string substitution

February 2, 2005 at 13:12:12
Specs: Win 2K, Pentium4 / 512M

I have a question with regards to variable substring substitution in an NT batch program.

For example, the following piece of code works as expected and replaces 'ab' with 'xy'.

::Edit and replace the character string 'ab' with 'xy'
SET v_test=123abcd
Set v_replacement=xy
SET v_result=%v_test:ab=xy%
ECHO %v_result% = 123xycd

How can I perform the substitution, when the replacement string is a variable instead of the contant string 'xy'
i.e. I want to replace 'ab' with the contents of the variable %v_replacement%, instead of the hardcoded value 'xy' ?

Thanks.


See More: Batch string substitution

Report •


#1
February 2, 2005 at 14:00:25

The command prompt as to be executed with
ENABLEDELAYEDEXPANSION for the code below
to work.
1, Right click the "Command Prompt" as add
the "/v" in the Target box as below.
and run the code.

%SystemRoot%\system32\cmd.exe /v

Tested with WinXP Pro.

@echo off
SET v_test=123abcd
Set v_replacement=xy
SET v_result=%v_test:ab=!v_replacement!%
ECHO %v_result%=123xycd


Report •
Related Solutions


Ask Question