I am using below mentioned code for finding a string in a file(Constants.TcGVL), assign my desired value(%select%) to it and copy to a new location:
Call :ValueSubstitude "%root%\LeafSpring\Constants.TcGVL" "%root%\LeafSpring\GVLs\Constants.TcGVL" "%select%"
:ValueSubstitude
@echo off
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
Set "Tag1=nNrOfInstances : UINT := "
Set "Tag2=%~3"
for /f "delims=" %%a in (%~1) do (
Set "str=%%a"
setLocal EnableDelayedExpansion
Set str=!str:%Tag1%=%Tag1%%Tag2%!
echo !str!>>"%~2"
ENDLOCAL
)
Exit /B 0
Original file has string like this:
nNrOfInstances : UINT := 3;
Now my %select% value could be 1, 2 ,3...and I want to replace '3' in above line with my desired number but the output I am getting is wrong.
The output looks like this:
=nNrOfInstances : UINT := 1= 3;
It has a space and equal sign at start of line and = 3 at the end. It has my desire number in it which is 1 but also the earlier value = 3 which should not be there.
Kindly assist me getting the right output or let me know what am I doing wrong here. Thank you.
%1,%2, and%3are, and exactly how those are passed to the batch file or:ValueSubstitudelabel.You should also provide a little information about the file, (assumed to be,%1); for instance its data type, encoding and line ending details.if "!str:~0,23!"=="nNrOfInstances : UINT :" (>>"%~2" echo nNrOfInstances : UINT : %Tag2%) else (>>"%~2" echo !str!)=sign cannot be replaced using substring modification. one solution would be to use aFor /floop to split the returned string using the optionstokens=1,2* delims==