1

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.

4
  • We expect a minimal reproducible example. That includes telling us exactly what the arguments %1, %2, and %3 are, and exactly how those are passed to the batch file or :ValueSubstitude label.You should also provide a little information about the file, (assumed to be, %1); for instance its data type, encoding and line ending details. Commented Feb 12, 2024 at 21:29
  • String replacement within variable expansion will not allow you to replace an equal symbol which is why you are getting said output. Commented Feb 12, 2024 at 21:39
  • I think the best you could without using a third party utility would be if "!str:~0,23!"=="nNrOfInstances : UINT :" (>>"%~2" echo nNrOfInstances : UINT : %Tag2%) else (>>"%~2" echo !str!) Commented Feb 12, 2024 at 21:47
  • your problem is likely arising from the fact that the = sign cannot be replaced using substring modification. one solution would be to use a For /f loop to split the returned string using the options tokens=1,2* delims== Commented Feb 13, 2024 at 4:01

1 Answer 1

2
@ECHO OFF
SETLOCAL

SET "tag2=2"
Set "Tag1=nNrOfInstances : UINT := " 
for /f "delims=" %%e in (q77984148.txt) do (
 ECHO %%e|FINDSTR /b /L /c:"%tag1%" >NUL
 IF ERRORLEVEL 1 (ECHO %%e) ELSE (
 ECHO %tag1%%tag2%;)
)

I used a file named q77984148.txt containing your data and some junk data for my testing.

The replacement processing uses findstr to see whether the line read (in %%e) begins with (/b) the literal string (/L) constant (/c:"this string"), and suppressing the output (>nul). This sets errorlevel to 0 if found and 1 if not.

If the resultant errorlevel is 1 or greater, simply echo the line, else (the string was found) so reconstruct the line (including the terminal ; which appears to have become lost in your narrative)

Note that the construction

(
for....do.(
 ....
)
)>filename

will recreate filename with the contents of (whatever the for loop outputs).

Naturally >>filename will append this data to an existing file filename or create that file if it doesn't already exist

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.