4

I have a batch file I'm using to search for a piece of text and replace it with another. It works, but what's happening is that when the 'new' file is created, anything after an exclamation mark is deleted.

So original file

Hello! I have some cheese

Just becomes

Hello

Although the text I am trying to replace is fine.

I understand that since I'm using delayed expansion I need to somehow escape the exclamation marks with ^^! but can't figure out where to do this. Adding it at the echo just echoes the ^^! literally.

Any help would be appreciated.

set "rootname=Common Filename"

set "replace="old">"
set "replaced="new">"


set "source="%rootname%_unqiue_suffix.txt""
set "target=Fixed\%SOURCE%"


 setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!
   )
) > %target%

endlocal
phuclv
  • 37,963
  • 15
  • 156
  • 475
SwagBag
  • 43
  • 2
  • 1
    there's neither enableDelayedExpansion nor `for /f` in DOS. [The Windows cmd is **not** DOS](https://superuser.com/q/451432/241386) – phuclv Nov 12 '18 at 14:44
  • Apologies. I'm not a coder or programmer and so I get things confused sometimes. – SwagBag Nov 12 '18 at 15:05

1 Answers1

4

To avoid loss of exclamation marks, enable delayed expansion only when it is really needed and expand normal % variables and for meta-variables only when delayed expansion is disabled:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "rootname=Common Filename"

set "replace="old">"
set "replaced="new">"

set "source=%rootname%_unqiue_suffix.txt"
set "target=Fixed\%source%"

(
    for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%source%"') do (
        set "line=%%b"
        setlocal EnableDelayedExpansion
        if defined line set "line=!line:%replace%=%replaced%!"
        echo(!line!
        endlocal
    )
) > "%target%"

endlocal

This code still causes trouble in case the variables replace and replaced contain !-signs, because they are percent-expanded.

aschipfl
  • 33,626
  • 12
  • 54
  • 99