brasqo
brasqo

Reputation: 1

Batch Script to replace line in an .ini

Please excuse me. I'm a complete noob.

I have 1 ini file with only 1 IP address and a version number.in this format:

xxx.xxx.xxx.xxx
abcde 

I have 3 total IP's that I need to accommodate. I'm looking to create a batch script that will ask a user for a location they'd like to connect to (each location would correspond to an IP address).

So for example: NY = 10.0.0.0 DC = 20.0.0.0 LA = 30.0.0.0

The batch would say "Where do you want to connect? 1= NY, 2= DC, 3= LA" When a user selects 1 for "NY", the script will search the .ini file (which is always in the same location c:\sample) and change the IP to the correct one (10.0.0.0). It would have an output of something like "You're now connected to NY!"

When a user selects "DC", the script will search the .ini file and change it to 20.0.0.0 etc.I'm able to do a simple find/replace script, but only with 2 IPs, and I had to make one for each location which was rather inconvenient.

Any help or guidance is much appreciated!

Upvotes: 0

Views: 1828

Answers (2)

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11151

This example will:

  1. Ask user to enter NY, DC or LA;
  2. Create x.tmp which is a copy of x.ini, replacing all xxx.xxx... with argument;
  3. Replace exisitng x.ini with x.tmp;

Code:

@Echo Off

:Begin
If Exist c:\sample\x.tmp Del c:\sample\x.tmp
Set /P "var=Choose location (NY, DC, LA):"
If /I "%var%"=="NY" Call :ReplaceIP 10.0.0.0
If /I "%var%"=="DC" Call :ReplaceIP 20.0.0.0
If /I "%var%"=="LA" Call :ReplaceIP 30.0.0.0
If Exist c:\sample\x.tmp (
    Move /Y c:\sample\x.tmp c:\sample\x.ini 1>Nul
    Echo Success!
) Else (
    Echo Invalid option!
)
Pause
GoTo :Begin

:ReplaceIP
For /F "Tokens=1,2,3,4 Delims=." %%i In (c:\sample\x.ini) do If %%j.==. (
    Echo %%i >> c:\sample\x.tmp
) Else (
    Echo %1 >> c:\sample\x.tmp
)
GoTo :EOF

Upvotes: 1

foxidrive
foxidrive

Reputation: 41224

This should help you out.

@echo off
set "var="
set /p "var=Enter 1 for NY, 2 for DC or 3 for LA"
if "%var%" EQU 1 (
  >c:\sample\file.ini echo 10.0.0.0
  >>c:\sample\file.ini abcde
  )
if "%var%" EQU 2 (
  >c:\sample\file.ini echo 20.0.0.0
  >>c:\sample\file.ini abcde
  )
if "%var%" EQU 3 (
  >c:\sample\file.ini echo 30.0.0.0
  >>c:\sample\file.ini abcde
  )
echo Good luck - I hope you entered the right number! :)

Upvotes: 0

Related Questions