Find and Replace String causing unknown error in .bat (batch file)
The passwords contains closing parathesis which is seen as a closure of the code block. You need to escape the occurrence of each )
In the below code snipped, I added the replacement section:
set "encrypted_pw=%encrypted_pw:)=^)%"
set "replace=%encrypted_pw%"
set "databaseFile=file.yml"
set "search=%password%"
for /f "delims=" %%i in ('type "%databaseFile%" ^& break ^> "%databaseFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%databaseFile%" echo(!line:%search%=%replace%!
endlocal
)
pause
Alterantively, and even better you can make it even shorter and let it be part of the replacement itself in the loop:
set "replace=%encrypted_pw%"
set "databaseFile=file.yml"
set "search=%password%"
for /f "delims=" %%i in ('type "%databaseFile%" ^& break ^> "%databaseFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%databaseFile%" echo(!line:%search%=%replace:)=^)%!
endlocal
)
pause
EDIT: to retain open lines.
@echo off
set "search=word to search"
set "replace=to replace"
set "databaseFile=file.yml"
for /f "delims=" %%i in ('type "%databaseFile%" ^| find /n /v "^" ^& break ^> "%databaseFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*]=!"
>>"%databaseFile%" echo(!line:%search%=%replace%!
endlocal
)
pause