I tried many examples online, but was not able to make it work. I am new to this so please forgive my primitive question.
say I have this:
curl "www.google.com" -o /dev/null -s -w "%{http_code}\n"
when running this in a cmd window, it returns the response code.
200
which is as expected.
I would like to do something like this in a batch script, but also use the output of the curl command to set a variable. All examples I found online propose:
set res = $(curl "www.google.com" -o /dev/null -s -w "%{http_code}\n")
echo %res%
pause
or like this
FOR /F "tokens=* USEBACKQ" %%F IN (`curl www.google.com -o /dev/null -s -w "%{http_code}\n"`) DO ( SET var=%%F )
ECHO %var%
pause
both did not work, my batch script closes immediately.
Any feedback or help is highly appreciated thank you.
For /F %G In ('%__AppDir__%curl.exe -s -o NUL "https://www.google.com" -w "%{http_code}\n"') Do @Set "var=%G". If you want to do it in a batch file, then using the advice in theforcommand help information,for /?, which states "To use the FOR command in a batch program, specify %%variable instead of %variable.", you'd do it like this,@For /F %%G In ('%__AppDir__%curl.exe -s -o NUL "https://www.google.com" -w "%%{http_code}\n"') Do @Set "var=%%G". Note: I don't recommend using this domain for any reason%-symbol in front of{http_code}need to be escaped by doubling it, like%%{http_code}…