Loop through file to find and replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rdekanter
Posts: 2
Joined: 23 May 2019 03:29

Loop through file to find and replace

#1 Post by rdekanter » 23 May 2019 03:47

Hello,

I am trying my first attempt at a simple batch script and am most of the way there but am struggling with getting the terminal window to correctly keep the user up to date with what's going on.

The script below works in terms of replacing text in a file, however I would like it to echo out a message once if the string is found and replaced. At the moment it appears to always run the else command so any echo placed there is outputted for each line.

Code: Select all

@echo off
:: This script toggles text
title Text Toggler

:: Set variables
set iniFile=C:\Program Files (x86)\Client.ini
set onPrem=192.168.40.108,192.168.40.109
set ultra=ultra.companyname1.net
:: N.b. I have anonymised the company name but the string length is the same

for /f "delims=" %%i in ('type "%iniFile%" ^& break ^> "%iniFile%" ') do (
    set line=%%i
    setlocal enabledelayedexpansion
    if !line:~-22!==%ultra% (
        >>"%iniFile%" echo(!line:%ultra%=%onPrem%!
        :: I would like to echo here if the line is found. Even better would be a counter that gets incremented, then if said counter is >0 at end of script can then echo once 
        :: there
    ) else (
        if !line:~-29!==%onPrem% (
            >>"%iniFile%" echo(!line:%onPrem%=%ultra%!
        :: Similar to above, I would like to echo here if the line is found. Again, as multiple replacements may happen a better solution would use a counter and then
        :: at the end of the script
        )
    )
    endlocal
)
Pause
An example .ini file is as follows:
[System]
Encryption.Scheme=on
MultiInstance.Enabled=false
Provisioning.Broker=true
Provisioning.IP=192.168.40.108,192.168.40.109
Encryption.ForcePeerVerification=false
Encryption.DisableSignatureVerification=true

[Transfer]
Transfer.IP=192.168.40.108,192.168.40.109

[Client]
Paste.Allowed=true
So to summarise, I simply want to toggle between having the IP addresses and the DNS address, then echo which one is now in the file (e.g. echo DNS active)

thanks for any help

aGerman
Expert
Posts: 3623
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Loop through file to find and replace

#2 Post by aGerman » 23 May 2019 10:50

Code: Select all

set /a "flag_ip=0, flag_dns=0"
for /f "delims=" %%i in ('findstr /n "^" "%iniFile%" ^& break ^> "%iniFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if "!line:~-22!"=="%ultra%" (
        >>"%iniFile%" echo(!line:%ultra%=%onPrem%!
        set /a "flag_ip=1"
    ) else if "!line:~-29!"=="%onPrem%" (
        >>"%iniFile%" echo(!line:%onPrem%=%ultra%!
        set /a "flag_dns=1"
    ) else >>"%iniFile%" echo(!line!

    for /f "tokens=1,2" %%j in ("!flag_ip! !flag_dns!") do endlocal &set /a "flag_ip=%%j, flag_dns=%%k"
)

if %flag_ip%==1 echo changed to IP
if %flag_dns%==1 echo changed to DNS
I don't know how risky it is to overwrite the ini file in the for /f clause already. A certain amount of text gets buffered before the loop begins to iterate. And maybe it's good enough for only some lines of text. Perhaps someone else can give some information on that.

Steffen

rdekanter
Posts: 2
Joined: 23 May 2019 03:29

Re: Loop through file to find and replace

#3 Post by rdekanter » 29 May 2019 06:12

Thanks so much for your help!

I couldn't get your code to work for me though it did give me enough to adapt what I had so that it now does what I want. For reference, my full code is as follows:

Code: Select all

@echo off
:: Set variables
set iniFile=C:\Program Files (x86)\Client.ini
set IP=192.168.40.108,192.168.40.109
set DNS=ultra.companyname1.net
set /a flag_IP=0
set /a flag_DNS=0

:: For loop will skip blank rows. Therefore use findstr first which appends a line number to each line,
:: then split into two tokens and only write the second part
for /f "tokens=1,2 delims=:" %%i in ('findstr /n "^" "%iniFile%" ^& break ^> "%iniFile%" ') do (
    set line=%%j
    setlocal enabledelayedexpansion
    if "!line:~-22!"=="%DNS%" (
        >>"%iniFile%" echo(!line:%DNS%=%IP%!
        set /a flag_IP=1
    ) else (
        if "!line:~-29!"=="%IP%" (
                >>"%iniFile%" echo(!line:%IP%=%DNS%!
                set /a flag_DNS=1
        ) else (
            >>"%iniFile%" echo(!line!
            )
    )
)
if %flag_DNS%==1 (
    echo DNS selected && echo.
) else (
    if %flag_IP%==1 (
        echo IP selected && echo.
    ) else (
        echo Something unexpected happened. Please check your .ini file settings
    )
)

Pause
                                                        

Post Reply