-4

I want to write a batch script, that will allow to search a particular ip and change it to a new ip address in a .ini file.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shiva r
  • 5
  • 2
  • Possible duplicate of [How can you find and replace text in a file using the Windows command-line environment?](http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir) – Mofi Nov 01 '15 at 09:19

1 Answers1

0

Try below updated code, please let me know if you still face issues

 cls 
 @Echo Off 
 SetLocal EnableDelayedExpansion 
 if exist new.ini ( del /s /q new.ini ) 
 set "search=1.1.1.1"
 set "search2=2.2.2.2"
 set "replace=7.7.7.7"
 set "replace2=8.8.8.8"
 for /F "tokens=*" %%a in (Apservice.ini) Do ( 
 Set "filevalue=%%a" 
 Set filevalue=!filevalue:%search%=%replace%!
 Set filevalue=!filevalue:%search2%=%replace2%!
 Echo !filevalue!>>new.ini 
 ) 
 move /y new.ini Apservice.ini 
 ause
prudviraj
  • 3,634
  • 2
  • 16
  • 22
  • Thanks prudviraj...i will try it out and let you know. – Shiva r Oct 09 '15 at 18:54
  • please note that you need to replace ip.ini with your .ini file name , i have tested this code and it is working fine , please let me know if u face issues – prudviraj Oct 09 '15 at 19:26
  • prudviraj, I tried the code and it is not working. It is getting stuck in the for loop. Essentially, there are four different services in the .ini file and I only need to change the IP for one of the services. How can I do that? – Shiva r Oct 13 '15 at 18:19
  • Actually, it does not replace with new ip address values. Below is the code I have. – Shiva r Oct 13 '15 at 19:00
  • cls @Echo Off SetLocal EnableDelayedExpansion if exist new.ini ( del /s /q new.ini ) set "SERVICE_L2TP_PEER_IPADDRESS=1.1.1.1" set "REPLACE_SERVICE_L2TP_PEER_IPADDRESS=7.7.7.7" echo search for !SERVICE_L2TP_PEER_IPADDRESS! ApService.ini echo replace !REPLACE_SERVICE_L2TP_PEER_IPADDRESS! ApService.ini if match found for /F "tokens=*" %%a in (Apservice.ini) Do ( Set "filevalue=%%a" Set filevalue =!filevalue:%SERVICE_L2TP_PEER_IPADDRESS%=%REPLACE_SERVICE_L2TP_PEER_IPADDRESS%! Echo !filevalue!>>new.ini ) move /y new.ini Apservice.ini – Shiva r Oct 13 '15 at 19:01
  • i have solved the problem , it is working fine , issue is with space near set command – prudviraj Oct 14 '15 at 06:21
  • Thanks a lot prudviraj…One other question, I do have one more IP to change that is part of different service name within the same .ini file. This IP has the same string name as well. How do I include that as well within this code? For e.g. Service Name=ABC SERVICE_L2TP_PEER_IPADDRESS=1.1.1.1 (Needs to change to 7.7.7.7) (Working as per above) Service Name=XYZ SERVICE_L2TP_PEER_IPADDRESS=2.2.2.2 (Needs to change to 4.4.4.4) – Shiva r Oct 14 '15 at 14:14
  • @Shiva r ,it would be easy for me to do that, i would recommend you to try , you have the required code, just need to modify a bit to meet your requirement , give it a try and if u are stuck at some point then revert . – prudviraj Oct 15 '15 at 06:27
  • I tried but it just changes one value only. any pointers will be greatly appreciated. – Shiva r Oct 15 '15 at 16:42
  • @Shiva r , adding one more search and replace variables similar to the one we did above would help , if that is not the case , then give your exact requirement with inputs details , i will look into it and see a way out. :) – prudviraj Oct 20 '15 at 09:12
  • Prudviraj, Sure thanks. But my issue here is –That when I add a new search and replace string it only takes precedence for one of the service. How do I specify which service to use from the file. I have two different Service names within the same .ini file and I need to replace with different IP address in both services. I researched in this forum and others but did not get any clues. Can you please help? – Shiva r Oct 22 '15 at 13:52
  • Prudviraj,Both are the same string.Prudviraj, Essentially, both strings are have the same name. SERVICE_L2TP_PEER_IPADDRESS. The file is Service_Name=Test 1 SERVICE_L2TP_PEER_IPADDRESS=1.1.1.1 Service_Name=Test 2 SERVICE_L2TP_PEER_IPADDRESS=2.2.2.2 Essentially, 1.1.1.1 will be replaced by 7.7.7.7 and 2.2.2.2 will be replaced by 8.8.8.8. – Shiva r Oct 22 '15 at 14:27
  • hi , if the same pattern needs two different values to be replaced then it would be a difficult task, but to better understand you problem and sneak a way through to find a solution provide complete details , then will try some way out – prudviraj Oct 26 '15 at 06:18
  • Hi Prudviraj, I am once again providng the details. In the same .ini file, I have the following. SERVICE_NAME=Test1 SERVICE_L2TP_PEER_IPADDRESS=1.1.1.1 SERVICE_Name=Test2 SERVICE_L2TP_PEER_IPADDRESS=2.2.2.2 It’s the same pattern, but different service name in the same file. The script will locate values above and change 1.1.1.1 to 7.7.7.7 and 2.2.2.2 to 8.8.8.8 – Shiva r Oct 27 '15 at 17:36
  • @Shiva r,done, try the updated code .. please vote up answer if it has helped you – prudviraj Oct 30 '15 at 10:40
  • Thanks prudviraj...Accepted the code...Really appreciate all the help you have provided. – Shiva r Oct 30 '15 at 22:37