FOR /F variable problems

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

FOR /F variable problems

#1 Post by ©opy[it]®ight » 14 Aug 2012 11:12

Hiya DosTips-members,

I'm facing an issue i have with a FOR /F loop.

I'm executing the beneath code but the variable %IP% doesn't get populated.
I need to run the loop in a new window (minimized), so that the main program doesn't get delayed.

Code: Select all

START "stats" /MIN CMD.EXE /C FOR /F %%I in ('..\..\exe\curl.exe http://icanhazip.com') DO (@SET IP=%%I
   echo Inside loop
   ECHO %%I
   ECHO %IP%
)

echo.
ECHO Outside loop
ECHO %IP%
echo.
PAUSE
EXIT


The problem is that the variable %IP% stays empty (it shows ECHO is off (UIT)) and only gets its value if i run the FOR loop in the same window as the main program (e.g. START "" /B or not using the START command at all), but then the remaining batch code will only be run when the FOR loop is finished, what i don't want.

Who has/knows the answer/solution?

Thanks.
Last edited by ©opy[it]®ight on 14 Aug 2012 12:06, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: FOR /F variable problems

#2 Post by abc0502 » 14 Aug 2012 11:31

if you wan't to get the ip and assign it to a variable see this topics:
viewtopic.php?f=3&t=3560
and foxidrive provided a simple vbscript in a batch that will assign the ip to a variable
viewtopic.php?p=18333#p18333

you can put that code in a separate batch and run it from your batch

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: FOR /F variable problems

#3 Post by Squashman » 14 Aug 2012 11:42

or this

Code: Select all

for /f "tokens=2 delims=[]" %%G in ('ping icanhazip.com ^| find "["') do set IP=%%G

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: FOR /F variable problems

#4 Post by abc0502 » 14 Aug 2012 11:46

Squashman wrote:or this

Code: Select all

for /f "tokens=2 delims=[]" %%G in ('ping icanhazip.com ^| find "["') do set IP=%%G

but that will give him the website ip, he want to get his external ip

but you can also do this:
get the page using the curl.exe
and then using a for loop to get the ip from the page and assign it to a variable %IP%
so there is no need for the other minimzed cmd window and avoid this problem

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: FOR /F variable problems

#5 Post by ©opy[it]®ight » 14 Aug 2012 11:57

Thanks abc0502 and Squashman, i know there are more ways to achieve the thing i want, but
the main problem (getting %IP% populated OUTSIDE the loop) still stands.

abc0502 wrote:but you can also do this:
get the page using the curl.exe
and then using a for loop to get the ip from the page and assign it to a variable %IP%
but that's what i did in the first place :?

I really want commands that connect to an external server/website to run in a seperate window, as a host/website can go offline, or pinging it could be slow for some reason, which would add more delay to remaining script's commands if it was run in the main script's window.

Hopefully i've explained it better this time :)
Last edited by ©opy[it]®ight on 14 Aug 2012 12:07, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: FOR /F variable problems

#6 Post by abc0502 » 14 Aug 2012 12:07

try to make your batch make another batch that has the for loop and start it from your main batch minimized

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: FOR /F variable problems

#7 Post by ©opy[it]®ight » 14 Aug 2012 12:10

Hmm, yes. That would be my final bet ;)

Is there really no (other) way to transfer/export a FOR loop result outside a loop, without losing the value of the variable?

Thanks for thinking along :-)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: FOR /F variable problems

#8 Post by abc0502 » 14 Aug 2012 12:19

I think i found a way just a second testing it.


Edit
sorry about that
I had an idea but found this
Changes made using the SET command are NOT permanent, they apply to the current CMD prompt only

and to make it permanent must use the SetX

The full topic here

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: FOR /F variable problems

#9 Post by Squashman » 14 Aug 2012 12:57

But then you would need to make sure you removed that environmental variable at the end of your script. You probably don't want that lingering around.
If you really have to have it in a separate cmd instance then I think your best bet would be to write it to a temp file and then read it back into a variable in your Main Batch file using SET /P redirection. But then again you may want to delete the temp file.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: FOR /F variable problems

#10 Post by ©opy[it]®ight » 14 Aug 2012 13:10

Atleast you two helped me into the right direction. I guess creating a temporary file won't hurt, but using SetX requires me (or others who use the script) to install extra software which is something i don't want ;-)

Thanks for the answers people! :)

Post Reply