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 s
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
)
)
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.
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