0

I'm trying to create a batch file that replaces a line in a text file.

The line is: Window Resolution:

Depending on some settings in my program, this line can look like this:

Window Resolution: 1

Window Resolution: 2

Window Resolution: 3

I need to replace Window Resolution: (number) with Window Resolution: 1 using a batch.

My first guess would be to create something like this:

@echo off
setlocal enableDelayedExpansion

:Variables
set InputFile=configfile.txt
set OutputFile=tempconfigfile.txt
set "_strFind=Window Resolution:"
set "_strInsert=Window Resolution: 1"

:Replace
>"%OutputFile%" (
  for /f "usebackq delims=" %%A in ("%InputFile%") do (
    if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
  )
)

DEL %InputFile%
MOVE %OutputFile% %InputFile%

ENDLOCAL

But I can't get this working... Any advice would be awesome. Thank you.

0

1 Answer 1

1

Your problem is if "%%A" equ "%_strFind%": Window Resolution: x will never be equal to Window Resolution:. Replace your if approach with:

echo %%A|findstr /bc:"Window Resolution:" >nul &&echo %_strInsert%||echo %%A
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.