user3629292
user3629292

Reputation: 5

BAT that retrieves IP and pads 0's

I've tried in so many ways to accomplish this, to retrieve an IP via a batch script, then stripping the dots (.) and padding all the sub segments in a length of 3 numbers all the time, for example:

192.168.59.1 should be 192168059001

So far I've come up with this:

set "str2=.pfx"
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n  1 ^| findstr "["') do (set thisip=%%a)
SET _result=%thisip:.=%
SET "ip=%_result%%str2%"

And instead, I get : _result=192168591, I've tried to add the 00 in the following way:

set "str2=.pfx"
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n  1 ^| findstr "["') do (set thisip=00%%a)
SET _result=%thisip:.=00%
SET "ip=%_result%%str2%"

And I get: _result=00192001680059001

How do I keep a fixed lenght of 3 digits for each subsegment?

Upvotes: 0

Views: 242

Answers (5)

MC ND
MC ND

Reputation: 70923

Just to include a solution without substring operations

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "host=%~1" & if not defined host set "host=localhost"

    for /f "tokens=2 delims=[]" %%z in ('ping -4 -n 1 %host%') do (
        for /f "tokens=1-4 delims=." %%a in ("%%z") do for /f "delims=" %%e in ('
            for %%f in (%%a %%b %%c %%d^) do @(
                set/a"%%f/100"^&set/a"%%f%%100/10"^&set/a"%%f%%10"
            ^)
        ') do echo Host [%host%] = %%z = %%e
    )

Upvotes: 0

Ryan Bemrose
Ryan Bemrose

Reputation: 9266

Here is a one-line PowerShell solution:

PowerShell.exe -c "'{0:000}{1:000}{2:000}{3:000}' -f [BitConverter]::GetBytes([Net.Dns]::GetHostByName($env:COMPUTERNAME).AddressList[0].Address)[0..3]"

[Net.Dns]::GetHostByName($env:COMPUTERNAME) uses .NET's GetHostByName function to get the IP address of the local computer. This script then takes the first element of the AddressList field and gets its integer Address property. The GetBytes method then converts this into an array of bytes. The first 4 of those are then formatted using the .NET format string {N:000} to print as an integer with zero padding to three decimal places.

This method uses the DNS APIs to look up the address, meaning it does not send a ping and wait it to return.

The downside to calling out to PowerShell is that loading the .NET framework into memory can add some delay (though much less than running PING). This is only a problem if you are not running any .NET, C#, or PowerShell applications on the same machine.

Upvotes: 0

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

for /F "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1') do set "thisip=%%a"
set "_result="
for %%a in (%thisip:.=,%) do set "part=00%%a" & set "_result=!_result!!part:~-3!"
echo %_result%

Upvotes: 1

Compo
Compo

Reputation: 38604

Try this:

@ECHO OFF
FOR /F "TOKENS=2-5 DELIMS=[.]" %%A in ('PING -n 1 "%COMPUTERNAME%" -4') DO (
    IF DEFINED ONE GOTO :EndIt
    SET/A ONE=100%%A, TWO=100%%B, THREE=100%%C, FOUR=100%%D
    CALL SET MyIP=%%ONE:~-3%%%%TWO:~-3%%%%THREE:~-3%%%%FOUR:~-3%%)
:EndIt
ECHO=%MyIP%
TIMEOUT -1 1>NUL

Upvotes: 0

Stephan
Stephan

Reputation: 56180

for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n  1 ^| findstr "["') do (set thisip=%%a)

for /f "tokens=1-4 delims=." %%a in ("%thisip%") do (
  set a=00%%a
  set b=00%%b
  set c=00%%c
  set d=00%%d
)
echo %a:~-3%%b:~-3%%c:~-3%%d:~-3% 

Upvotes: 1

Related Questions