TroggleDorf
TroggleDorf

Reputation: 420

Concatenating lines together in a batch file

I have a machine with multiple IP addresses. I'd like to write a batch script that can join these IP addresses together. So far I've managed to parse IPCONFIG and pull each IP address out line by line:

@echo off
setlocal EnableDelayedExpansion

FOR /F "tokens=*" %%a IN ('ipconfig ^| findstr /i "ipv4"') do (
echo Line - %%a
)

That gives me the following output:

Line - IPv4 Address. . . . . . . . . . . : x.x.x.x
Line - IPv4 Address. . . . . . . . . . . : y.y.y.y

What I'd like to do is take the IP addresses at the end of each line and end up with one output line like the following:

IPs=x.x.x.x;y.y.y.y

I am not sure how to proceed. Any suggestions would be appreciated.

Thanks!

Upvotes: 2

Views: 886

Answers (1)

user6811411
user6811411

Reputation:

  • The 1st for splits the line at the colon,
  • the 2nd for trims spaces.
  • Set "IPs=%IPs:~1%"` removes a leading semicolon

:: Q:\Test\2018\11\20\SO_53384288.cmd
@echo off
setlocal EnableDelayedExpansion
Set "IPs="
FOR /F "tokens=2delims=:" %%A IN (
    'ipconfig ^| findstr /i "ipv4"'
) do for %%B in (%%A) do Set "IPs=!IPs!;%%B"
Set "IPs=%IPs:~1%"
set IPs

Upvotes: 1

Related Questions