1

I am attempting to use a batch file to find an IP address in a .ini file in a given directory, and change it to a new address. I am suing this question as source.

The scripts runs without errors, however it creates replacement file that is empty and is also a .bat file, as opposed to a .ini.

Script:

set "file=M:\Haem\C.Mooney\cmooney\apex\Scripts\testIPCOnfig.ini"

:loop
findstr "^ipaddress=193.120.187.44$" "%file%" >nul || (
type "%file%" | repl "^ipaddress=193.120.187.44=.*" "ipaddress=193.120.187.110" >"%file%.tmp"
move "%file%.tmp" "%file%" >nul
)
ping -n 120 localhost >nul
goto :loop

The rpl command is taken form a helper file, sourced here.

The contents of the testIPCOnfig.ini file:

ipaddress=193.129.187.44

Appreciate any feedback.

UPDATED SCRIPT WITH UPDATED JREPL HELPER :

rem Check if the helper file is in same directory, if not exit


if not exist "%~dp0JREPL.bat" goto :EOF
rem if not exist "M:\Haem\C.Mooney\cmooney\apex\Scripts\IPChange\testIPCOnfig.txt" goto :EOF


rem call te helper JREPL>BAT to search for ipaddress and change it 

call "%~dp0JREPL.bat" "^ipaddress=\d+\.\d+\.\d+\.\d+" "ipaddress=193.120.187.110" /F "M:\Haem\C.Mooney\cmooney\apex\Scripts\IPChange\testIPCOnfig.txt" /O -

Contents of the testIPCOnfig.txt:

ipaddress=193.120.187.44

Returns following Error:

JScript runtime error opening input file: File not found

dancingbush
  • 2,131
  • 5
  • 30
  • 66

1 Answers1

2

REPL.BAT written by Dave Benham is deprecated and replaced by JREPL.BAT also written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.

@echo off
if not exist "%~dp0jrepl.bat" goto :EOF
if not exist "M:\Haem\C.Mooney\cmooney\apex\Scripts\testIPCOnfig.ini" goto :EOF

call "%~dp0jrepl.bat" "^ipaddress=\d+\.\d+\.\d+\.\d+" "ipaddress=193.120.187.110" /F "M:\Haem\C.Mooney\cmooney\apex\Scripts\testIPCOnfig.ini" /O -

The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks first if JREPL.BAT really exists in directory of the batch file and exits if this condition is not true.

Then the batch file checks existence of the INI file to modify and exits if this file does not exist at all. See Where does GOTO :EOF return to?

Then JREPL.BAT is called to search for a line starting with ipaddress= and four numbers with one dot between the numbers and replaces those lines with the specified replace string.

Mofi
  • 46,139
  • 17
  • 80
  • 143