1

I am in the process of creating a batch file that will find a users IP address and write it into a specific place in a file. The problem I'm encountering is where I put the ip value there is an extra space where I do not need one.

e.g.

for /f "usebackq tokens=2 delims=:" %%f in ('ipconfig ^| findstr /c:"IPv4 Address"') do @echo   "IPv4" : "%%f">>txt.properties

comes out like this " 10.10.10.555" I'm stumped with how to get rid of that extra space right after the quote.

jeb
  • 78,592
  • 17
  • 171
  • 225

2 Answers2

0

Capture the variable and use :~1 to read from the 2nd character:

setlocal EnableDelayedExpansion
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do (
  set ip=%%f
  @echo "IPv4" : "!ip:~1!">>txt.properties
)
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Thank you, have any time to explain what setlocal EnableDelayedExpansion does? It seems like !-indicate a modification to a specified variable, not sure what the : is for either in the !ip:~1! – Not_yes Waveer Jan 26 '15 at 06:50
0

I prefer to not set EnableDelayedExpansion, if I can help it (I come from an era where you could not always ensure it's availability):

for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"  IPv4 Address"`) do @for /f "tokens=1 delims= " %%g in ("%%f") do @echo "IPv4" : %%g

The spaces in front of IPv4 Address prevent a false positive on "Autoconfiguration IPv4 Address" that returns a 169 IP address.

John Castleman
  • 1,552
  • 11
  • 12