Mert
Mert

Reputation: 3

Set does not work in for loop scripting windows batch

I want to set IP Address to a variable but my code isn't working.

@echo off

setlocal EnableDelayedExpansion enableextensions

SET IP=
for /f " tokens=14 delims= " %%i in ('ipconfig ^| findstr /c:"IPv4 Address"') do  SET /P IP = %%i
call :foo  

:foo
SET _AA = IP   
echo _AA

Upvotes: 0

Views: 94

Answers (2)

Compo
Compo

Reputation: 38579

Here's one way to achieve your goal:

@Echo Off
Set "IP="
For /F "Delims=" %%A In ('IPConfig^|Find "IPv4"') Do Set "IP=%%A"
If Not Defined IP GoTo :EOF
Set "IP=%IP:*: =%"
Echo( Local variable %%IP%% resolves to %IP%
Timeout -1
GoTo :EOF

Upvotes: 0

Magoo
Magoo

Reputation: 79982

Spaces are significant on both sides of the = in a set

remove the spaces.

In order to reference the contents of a variable var you need to use %var%

Batch simply continues line-by-line until it reaches a goto or end-of-file. It has no concept of "procedures." Having executed your for, it will continue and once again execute the set and echo. You need a line

goto :eof

before the label :foo. This instruction goes to end-of-file (the colon in this case is required)

Upvotes: 2

Related Questions