1

I'm executing the following command to write the current IPv4 address to a logfile:

ipconfig | findstr IPv4>>"c:\ip.log"

However, there are some whitespaces, how can I remove those?:

   IPv4 Address. . . . . . . . . . . : 10.0.0.15
   IPv4 Address. . . . . . . . . . . : 172.16.0.15
   IPv4 Address. . . . . . . . . . . : 192.168.0.15

Thanks.

zach
  • 71
  • 1
  • 7
  • Related: [Write local ip address to text file using batch file](http://stackoverflow.com/questions/13856398/write-local-ip-address-to-text-file-using-batch-file) – Andriy M Feb 26 '15 at 10:23

4 Answers4

1
@ECHO OFF
SETLOCAL

FOR /f "tokens=1*delims=:" %%a IN ('ipconfig^|findstr "IPv4" ') DO (
 FOR /f %%c IN ("%%b") DO ECHO ++%%c++
)

GOTO :EOF

Naturally, the echo could be a set if you so desire. The ++ either end of the data is merely to show that the spaces have been removed.

Find the target line(s) from an ipconfig command, select that part beyond the : on those lines, and process the resultant string, removing ant spaces (default delimiters in the for /f %%c)

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

This is a nice command you could use: http://fart-it.sourceforge.net/ You would use it to find and replace the part that is waste for your purpose.

Edit If you want stick with the commands available, then i think you would best use this: How to replace substrings in windows batch file

Look at Andriy M's first answer.

Community
  • 1
  • 1
ZGFubnk
  • 500
  • 4
  • 13
  • thanks dannl, but I'd prefer to use commands default to Windows only and not any external tools. – zach Feb 25 '15 at 16:14
0

Maybe use ECHO and FOR to try something like this:

        ipconfig | findstr IPv4>>"c:\ip.log"
        < "c:\ip.log" SET /P my_first_ip=
        FOR /F "delims= tokens=2" %%I IN ("%my_first_ip%") DO (
        ECHO %%I>"c:\ip.log"
        )
HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
0

PowerShell:

get-wmiobject Win32_NetworkAdapterConfiguration -filter 'IPEnabled=TRUE' |
  select-object -expandproperty IPAddress | out-file "C:\IP.log" -append
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62