-2

On my CD-ROM I have this file: ip.txt which has the content:

ip="192.168.1.1" netmask="255.255.255.0" gateway="123.456.789.2"

How can I extract from ip.txt the IP, netmask and gw and use as command to setup the IP.

I know the command works like this: netsh interface ip set address name="Local Area Connection" static 192.168.0.100 255.255.255.0 192.168.0.1 1

But I do not know how to make a batch file to extract the info I need from the ip.txt file, do you know how that may be done?

Thanks.

Mark Riddell
  • 1,143
  • 1
  • 7
  • 11
Svan
  • 1
  • Hi Svan, welcome to Server Fault! Please be aware that we are not a script writing service and questions asking for scripts are generally frowned upon. If you have made an attempt yourself and are stuck at a particular point we will be happy to assist. – Mark Riddell Aug 20 '16 at 18:36

2 Answers2

1

Why Batch ? What about Powershell ?

This could give you a starting point to look around :

$content = gc C:\ip.txt
$ip = $content.split(" ")[0].split('"')[1]
$netmask = $content.split(" ")[1].split('"')[1]
$gw = $content.split(" ")[2].split('"')[1]
$command = "netsh interface ip set address name=`"Local Area Connection`" static $ip $netmask $gw"
Invoke-Expression $command

Certainly there is a more efficient way...but what did you try by yourself ?

krisFR
  • 13,280
  • 4
  • 36
  • 42
0

For /F tokens^=2-6^ delims^=^" %a in (IP.TXT) do netsh interface ip set address name="Local Area Connection" static %a %c %e

Should work, but PowerShell is a better long term bet.

%a will be the IP, %c the netmask and %e the gateway if you need to jiggle them round a bit.