2

I need a batch file that should search the PC for all files issued today named as "example.ini" and replaces a string which will be in the second line of "example.ini", it should replace

SERVER_IP=111.111.111.111 with another server ip SERVER_IP=222.222.222.222

How is that possible?

4
  • 1
    Is SERVER_IP= on any other line? Commented Nov 4, 2013 at 9:14
  • SERVER_IP= is on the second line only Commented Nov 4, 2013 at 9:18
  • Thanks. Searching an entire drive and checking each example.ini for todays date could be very slow. Is there a way to narrow down the search? Will the example.ini have any other distinguishing features? Maybe the IP address? Commented Nov 4, 2013 at 9:23
  • SERVER_IP=212.83.63.108 this is the main ip in "example.ini" Commented Nov 4, 2013 at 9:30

3 Answers 3

4

Test this on some sample files.

It will change the example.ini files on c:\ and in subdirectories which contain SERVER_IP=212.83.63.108 and will change it to SERVER_IP=222.22.22.222

It uses a helper batch file called repl.bat from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place repl.bat in the same folder as the batch file.

@echo off
for /r c:\ %%a in (example.ini) do (
find "SERVER_IP=212.83.63.108" <"%%a" >nul && (
    echo processing "%%a"
    type "%%a"|repl "SERVER_IP=212\.83\.63\.108" "SERVER_IP=222.22.22.222" >"%%a.tmp"
    move /y "%%a.tmp" "%%a" >nul
    )
)
Sign up to request clarification or add additional context in comments.

4 Comments

The Batch File Can Find The "example.ini" but when trying to replace i get error 'repl' is not recognized as an internal or external command, operable program or batch though i have repl.bat in same folder
I'd say that you are elevating the batch file with admin permissions. Place repl.bat in the c:\windows folder so it is on the path.
Thanks A Lot It Works Now It Was Able to replace the string in my "example.ini" thanks for all your effort
@admdrew The code for repl.bat is already hosted on Stack Overflow and also DosTips.com and including it every time it is used would create huge posts - so I rolled back your edit.
1

Change String1 by String2 in the second line of files named example.ini that has been created today in any subdirectory of drive c:

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem Arguments:
    rem     %1 = search string
    rem     %2 = replace string
    rem     [ %3 = file in where to replace ] 
    rem            this parameters is internally used by the 
    rem            batch file while processing

    rem chech for parameters
    if "%~2"=="" goto :EOF

    rem retrieve parameters
    set "_search=%~1"
    set "_replace=%~2"

    rem check if asked to replace in a file 
    rem this will be done recursively from this same batch
    if not "%~3"=="" (
        set "_file=%~3"
        goto doFileReplace
    )

    forfiles /P C:\ /S /M example.ini /D +0 /C "cmd /c if @isdir==FALSE findstr /c:\"%_search%\" \"@path\" >nul && %~dpnx0 \"%_search%\" \"%_replace%\" @path "

    goto :EOF

:doFileReplace
    echo "%_file%"
    set "_tempFile=%temp%\%~nx0.tmp"
    break > "%_tempFile%"
    (for /F "tokens=1,* delims=:" %%l in ('findstr /n /r "." "%_file%"') do (
        if %%l EQU  2 (
            set "_text=%%m"
            echo !_text:%_search%=%_replace%!
        ) else (
            echo %%m
        )
    ))>>"%_tempFile%"

    copy /Y "%_tempFile%" "%_file%" >nul 
    goto :EOF

Comments

0

On 32 bit windows Edlin (a dos editor) is available.

12 
New text
e

is an edlin script that writes "New text" on line 12 of a file. Type

edlin

then

?

To use

edlin c:\folder\filename.ext < edlin.script

You must use short filenames. File must be under 64K lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.