1

I want to get the outside temperature into a variable.

curl wttr.in/berlin?format=%t

produces a perfect clean output of, for example +8°C (at command prompt) that I'd like. %t is for temperature. however this

curl wttr.in/berlin?format=%t>temp.txt
set /p temp=<temp.txt

produces -7┬░C which I don't like. I just wonder if I could fix this for command instead and skip the character set problem

for /f %%x in ('curl wttr.in/berlin?format=%t') do set temp=%%x

but this one suddenly produces general multirow result instead of just the simple temperature, along with error

 Could not resolve host: %t

ultimately I will need multiple variables from wttr.in so it would be most efficient to extract them all at once, and set variables accordingly, for example

curl wttr.in/berlin?format="%t+%C+%w"

where %C is conditions, %w is wind. does this mean that fixing the for loop is the way to go for simplicity?

8
  • 3
    In your For loop, change format=%t to format^=%%t. BTW, in your previous code example, set /p temp=temp.txt should have read set /p temp=<temp.txt. Commented Jan 8, 2023 at 17:07
  • 2
    for /f "delims=" %%a in ('"curl wttr.in/berlin?format="%%t+%%C+%%w""') do echo %%a fixes your for problem. No solution for the weird characters though. Commented Jan 8, 2023 at 17:38
  • 3
    I wasn't giving you a solution to your encoding problem Bricktop, I was showing you how to correct both of your incorrect commands. Commented Jan 8, 2023 at 17:40
  • 2
    The problem is, curl returns a Unicode string. Writing it to an ASCII file "translates" the two Unicode bytes of ° into two bytes. Commented Jan 8, 2023 at 17:40
  • 1
    I honestly don't understand why you'd need to parse the known °C part anyhow, surely you only need the number, the rest can be added manually if required in later output. Commented Jan 8, 2023 at 17:43

1 Answer 1

1
chcp 65001
for /f "tokens=1-9,*" %%a in ('"curl wttr.in/berlin?format^="%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C""') do (
    set "temperature=%%a"
    set "tempfeel=%%b"
    set "moonday=%%c"
    set "precipitation=%%d"
    set "dawn=%%e"
    set "sunrise=%%f"
    set "zenith=%%g"
    set "sunset=%%h"
    set "dusk=%%i"
    set "condition=%%j"
)

Unicode fixes the unicode issue, ^ fixes the for loop, along with "tokens=1-9,*" for collecting all text based variables "%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C", where condition is a phrase, collected by the *.

Sign up to request clarification or add additional context in comments.

Comments

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.