I'm currently running a batch script to search through multiple different files for matching strings. When string A(foobar) matches B+C(foo+bar), then user is prompted to enter D(qux), replacing A with B+D(foo+qux).
During the search, the file will encounter multiple places where the script will replace the same A multiple times, but I don't want to prompt the user every time, only the first encounter.
Attempting at creating a dynamic function, I've supplied my function with 4 different parameters:
B (First part of string): foo
C (Second part of string, getting replaced): bar
X (File location): C:\application.properties
Y (Old stored B+D): fooqux
Y is essentially only supplied as parameter in order for the function to know which variable to overwrite (in my code: batchIp and globalUnitName)
SET %2 = %L%
should be possible.
https://www.dostips.com/DtTutoFunctions.php Shows that:
set "%~1=DosTips"
should be possible.
My code:
:replace_properties
set batchIp =
call :replace_string "batch.ip=http://" "192.168.68.132" C:\application.properties %batchIp%
call :replace_string "batch_ip = " "192.168.68.132" C:\processor.cfg %batchIp%
set globalUnitName =
call :replace_string "unit.name=" "Global_Default" C:\application.properties %globalUnitName%
:replace_string
@echo off
findstr /m "%~1%~2" %~3 >nul 2>nul
if %errorlevel%==0 (
if "%~4"=="" (
echo File: %~3
echo %~1%~2 was found, define new replacement:
set /p applicationProperty="? %~1"
IF NOT DEFINED applicationProperty SET "applicationProperty=%~2"
echo before: %~4
echo before: %batchIp%
echo before: %globalUnitName%
set %~4=!applicationProperty!
echo after: %~4
echo after: %batchIp%
echo after: %globalUnitName%
) else (
set applicationProperty="%~4"
)
powershell -Command "(gc %~3) -replace '%~1%~2' , '%~1!applicationProperty!' | Set-Content %~3"
set applicationProperty=
)
exit /B 0
I would expect that the variable batchIp inside :replace_properties would be replaced with the content of applicationProperty inside :replace_string when running set %~4=!applicationProperty!.
However,
echo before: %~4
echo before: %batchIp%
echo before: %globalUnitName%
set %~4=!applicationProperty!
echo after: %~4
echo after: %batchIp%
echo after: %globalUnitName%
turns into:
before:
before:
before:
The syntax of the command is incorrect.
after:
after:
after: