I have a text file in which I would like to change the commented strings to some other value, or vise versa. Commented strings in the text file begin with an exclamation mark (!). I am using the FindReplace function as mentioned in the following article: Batch script to find and replace a string in text file within a minute for files upto 12 MB
When I use the :FindReplace function with strings containing an !, I suspect that delayedExpansion is trying to interpret the ! as part of a variable, so when FindReplace is called, the entire line is missing in the file. I've tried to escape the ! in the string by using ^ or ^^, and it doesn't seem to work.
setlocal enableDelayedExpansion
REM //bunch of other script
REM . . . [rest of script not shown]
set /p user_name="Enter user name: %=%"
call :FindReplace "username :" "username: %user_name%" tmpfile.cfg
REM  Comments in txt file start with an !
call :FindReplace "!This is a comment" "This is no longer a comment" tmpfile.cfg
exit /b
:FindReplace 
::<findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
  for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
    <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
    if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
  )
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
How can I make this work correctly?
Edit (1): Looks like I need "enableDelayedExpansion" based on the following "if" statement below. If I have the "enableDelayedExpansion" commented out, the "if" block doesn't execute.
IF EXIST "%PROD_PATH%\%OS_VER%\bin\prod.exe" (
    ECHO There appears to be an installation of PROD in %PROD_PATH%
    SET /p overwrite_input="Would you like to overwrite this installation? [y/n] %=%"
    IF "%overwrite_input%" == "n" ( 
        ECHO Installation Terminating...
        ECHO -----------------------------------------------------------------------------------------------
        PAUSE
        EXIT /B
    ) ELSE IF "%overwrite_input%" == "y" ( 
        ECHO Uninstalling existing application...
        call :Uninstall "%PROD_PRE_COPY%"
        ECHO Continuing installation...
        ECHO -----------------------------------------------------------------------------------------------
    ) ELSE ( 
        ECHO Error: Operation Invalid. Please enter 'y' for yes or 'n' for no without quotes
        ECHO Installation Terminating...
        ECHO -----------------------------------------------------------------------------------------------
        PAUSE
        EXIT /B
    )
 )
 
     
     
     
    