How to replace @DLOG_ETS=ON by @DLOG_ETS=OFF in all *.opr and *.eng files?

52 views Asked by At

I've created a simple batch file that searches in files with extension .opr and .eng for the term @DLOG_ETS and replace it with @DLOG_ETS=OFF. This may be redundant for cases where the full string is @DLOG_ETS=OFF.

I want to add the condition that only if the string @DLOG_ETS=ON is present, then I want to replace that with @DLOG_ETS=OFF. Otherwise, I want to leave it as it is, i.e. @DLOG_ETS=OFF without making any changes on those .opr or .eng files.

for %%F in (*.opr  *.eng) do (
    type "%%F"| findstr /v @DLOG_ETS= >"%%F.new"
    @echo @DLOG_ETS=OFF  >> "%%F.new"
    move /y "%%F.new" "%%F"
)
2

There are 2 answers

2
Magoo On
@ECHO Off
SETLOCAL
for %%F in (*.opr  *.eng) do (
 findstr /v /L "@DLOG_ETS=ON" "%%F" >"%%F.new"
 fc "%%F" "%%F.new" >nul
 IF ERRORLEVEL 1 (
  echo @DLOG_ETS=OFF>>"%%F.new"
  move "%%F.new" "%%F"
 ) ELSE (
  DEL "%%F.new"
 )
)

GOTO :EOF

For each file, create a new file, removing the line containing the target string. Compare the before and after versions. If there is a difference therefore the string was removed, so append the required line. If no difference, simply delete the new file.

1
AudioBubble On

Batch isn't predestined to do text file editing.
A lot of poison chars have to be circumvented.
Vbscript /jscript/PowerShell are all better suited to this task.

Here a batch which calls a sub to get help with Powershell. You need do adapt the path to your environment.

@Echo off
PushD "X:\Path\to\your\folder"
Set "Srch=@DLOG_ETS=ON"
Set "Repl=@DLOG_ETS=OFF"
Set "Opt=-nologo -noprofile -command"
for %%F in (*.opr *.eng
) do findstr /I "%Srch%" "%%~fF" >NUL 2>&1 && Call :PoSh "%%~fF"
PopD
Goto :Eof
:PoSh
PowerShell %Opt% "& {(GC %1).replace('%Srch%','%Repl%')|Set-Content %1}"