0

enter image description hereI'm trying to SET one or two Variables in a for /f loop and use them outside the loop, but so far I have failed to do so, after hours of trying I finally gave up and ask for somebody to help me. I'm aware of the use of SETLOCAL ENABLEDELAYEDEXPANSION in a batch file but with or without using it it still does not work. The code so far is:

    @echo off
SETLOCAL ENABLEDELAYEDEXPANSION

curl -s 192.168.1.68/cgi-bin/home | for /f "tokens=1,2,3" %%a in ('Grep -o -P "[0-9].{0,8}kWh.{0,0}"') do (

    set VAR01=%%a
    set VAR02=%%a
)

echo %VAR01%
echo !VAR01!
ENDLOCAL

RESULT on Console is:

(set VAR01=2.14   & set VAR02=2.14  )

ECHO is off.

ECHO is off.

The variable is SET in the loop but outside the loop it is not.

2
  • Result of the echo %VAR01% and the echo !VAR01! is ""/blank,/nothing. Commented Nov 12, 2017 at 13:35
  • Yes unfortunately it is, why do you think it is not? Commented Nov 12, 2017 at 15:06

2 Answers 2

2

your problem is piping. Both sides of a pipe are executed in their own environment, so your variables are gone as soon as they are finished.

Watch the output of the following two methods (I used standard cmd commands, because I don't have curl and grep, but the result should be the same.

echo ON
setlocal enabledelayedexpansion

echo hello world|for /f "tokens=1,2" %%a in ('findstr "e o"') do (
  set a=%%a
  set b=%%b
)
echo %a% %b%
echo ---------
for /f "tokens=1,2" %%a in ('echo hello world^|findstr "e o"') do (
  set a=%%a
  set b=%%b
)
echo %a% %b%
Sign up to request clarification or add additional context in comments.

Comments

0

try

 set VAR01=%%a
 set VAR02=%%a
 goto done
)
:done
echo %VAR01%

It's hard to believe that your result is actually as you claim, since there is no & on a single line in your code.

I believe that the for is executing more than once - the first time it might set the variables, and the second time clear them. Again, this should be shown on your trace.

By gotoing out of the loop, the variables should be displayed directly after having been set the first time.

The conventional way to do this is

for ... in (`curl... ^|grep...') do (

where the ^ escapes the pipe to make it redirect within the quoted command.

2 Comments

@Maggo: was my first thougt also, but the output is indeed correct (caused by the pipe outside the for).
I have changed it into: SETLOCAL ENABLEDELAYEDEXPANSION curl -s 192.168.1.68/cgi-bin/home | for /f "tokens=1" %%a in ('Grep -o -P "[0-9].{0,8}kWh.{0,0}"') do ( SET VAR01=%%a SET VAR02=%%a SET VAR03=%%a goto :done ) :done echo %VAR01% echo %VAR02% echo %VAR03% ENDLOCAL But still the same result, VAR0x is set by the value of %%a inside the loop but outside it is still empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.