简体   繁体   中英

Windows Batch - Find a line and replace with text

probably this had been already answered in the Internet but I can't find the solution.

I've a .ini file which contains a lot of text, here is an example:

[General]
FullScreen = True
InternalResolution = 0

I need to setup a batch file that searchs for this line InternalResolution and changes the value for a different number, for example 5. The output should look like this:

[General]
FullScreen = True
InternalResolution = 5

I have two problems, the first one is that the value = 0 sometimes could be = 1, or = 2, and also that all the spaces have to exist in the file (an app depends of this ini file so I can't remove the spaces. I tried this:

@echo off
setlocal disableDelayedExpansion

:Variables
set InputFile=myfile.ini
set OutputFile=myfiletemp.ini
set "_strFind=InternalResolution = 0"
set "_strInsert=InternalResolution = 5"

: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

It works only if the InternalResolution value is 0. I don't know how to replace the line if the value is something different than 0. There should be a way to search if the line contains the text InternalResolution = X and replace the whole line or the value.

Thank you.

FIND is a good option. An easier way is to separate it into two tokens %%A %%B and just test if %%A is equal to %_strFind% (i also changed it).

@echo off

:Variables
set InputFile=myfile.ini
set OutputFile=myfiletemp.ini
set "_strFind=InternalResolution"
set "_strInsert=InternalResolution = 5"

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

MOVE /Y %OutputFile% %InputFile%

Tested on a Windows 10 laptop.
NOTE: Might not be able to handle poisonous characters

You could use sub-string substitution , like in the following:

:Replace
> "%OutputFile%" (
    for /F "usebackq delims=" %%A in ("%InputFile%") do (
        set "line=%%A"
        setlocal EnableDelayedExpansion
        if "!line!" equ "%_strFind%!line:*%_strFind%=!" (
            echo(!_strInsert!
        ) else (
            echo(%%A
        )
        endlocal
    )
)

Windows 10 64-bit. PowerShell 5.1

Replace text with PowerShell 5.1 using get-content and a regular expression.

There must be exactly one space before and after the equal sign.

InternalResolution = 0-9 is replaced with InternalResolution = 5

(Get-Content "myfile.ini") -replace "InternalResolution = \d", "InternalResolution = 5" | Set-Content "myfiletemp.ini"

See how to replace text with powershell, get-content,-replace, and regex

Regex

Windows 10 64-bit

Find and replace text with cmd's FOR and IF commands

Replaces:

InternalResolution = 0
InternalResolution      = 1
InternalResolution =      2
InternalResolution =      abc

With:

InternalResolution = whatever value you want

InternalResolution= whatever is not replaced.

@echo off
setlocal enableextensions
set InputFile=myfile.ini
set FindStr=InternalResolution
copy /y %InputFile% %InputFile%.bak
(for /f "usebackq tokens=1*" %%g in ("%InputFile%") do if "%%g" equ "%FindStr%" (echo %%g = 5) else (echo %%g %%h))> %temp%\# 
MOVE /Y %temp%\# %InputFile% 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM