I want to use a PowerShell CLI within my batch file to find and replace text under certain conditions. This is my code:
set "target=%cd%Folder 1Folder 2"
set "string=+some_text="Cat.Dog.Fox""
if /i "%input%"=="1" (
findstr /b /r /c:";%string%" "%target%Test.ini" >nul 2>nul
if "%errorlevel%"=="0" (
set "replace="
) else (
set "replace=;"
)
powershell -NoProfile -Command "$file = '%target%Test.ini'; $null = New-Item -Force $file -Value ((Get-Content -Raw -Encoding UTF8 $file) -Replace '(?m)^W*(%string%)', '%replace%$1')"
)
goto :eof
I want to replace +some_text="Cat.Dog.Fox"
in %cd%Folder 1Folder 2Test.ini
with ;+some_text="Cat.Dog.Fox"
when there’s no ;
before that text. If there is a ;
before +some_text="Cat.Dog.Fox"
I want to delete the ;
.
So basically
+some_text="Cat.Dog.Fox"
becomes ;+some_text="Cat.Dog.Fox"
and with the next run
;+some_text="Cat.Dog.Fox"
becomes +some_text="Cat.Dog.Fox"
again.
My issue is that my code only changes the text if there is no ;
before +some_text="Cat.Dog.Fox"
. It does delete the ;
if there is one.