I want to add my ip to system/user environment in a bat file.
This is the code of the bat file
curl http://httpbin.org/ip
This is the output:
{
"origin": "1.2.3.4"
}
I want to set environment set ip=1.2.3.4
How should I do that?
I want to add my ip to system/user environment in a bat file.
This is the code of the bat file
curl http://httpbin.org/ip
This is the output:
{
"origin": "1.2.3.4"
}
I want to set environment set ip=1.2.3.4
How should I do that?
curl http://httpbin.org/ip
This is the output:
{
"origin": "1.2.3.4"
}
Use the following batch file (test.cmd):
rem @echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1 tokens=2" %%i in (`curl http://httpbin.org/ip`) do (
setx ip %%~i
goto :done
)
:done
endlocal
Notes:
skip=1
is used to skip the first line {
.goto :done
is use to skip the last line }
.~
is used to remove the quotes from the IP address.set
instead of setx
and I had echo !ip!
after set
, which requires enableDelayedExpansion
. If it works without you can remove it.
set ip=
is temporary ... and will remain set only for the duration of the current cmd
shell. Your question said "system/user environment" which I understood to mean you wanted it permanently set.
This would be the equivalent in PowerShell :
$ip = curl http://httpbin.org/ip | Select-String -Pattern "[0-9.]+" | Select Matches
Set ip $ip.Matches.Get(0).Value