[SOLVED] Problem with getting values from request using batch

Issue

I’m new in batch and i need to get a tag from a url like:

tag_name in url https://api.github.com/repos/pmmp/PocketMine-MP/releases/latest
So i found that someone was doing this to get it :

for /f "tokens=1,* delims=:" %%A in ('curl -ks https://api.github.com/repos/pmmp/PocketMine-MP/releases/latest ^| find "tag_name"') do (
    SET VERSION=%%B
    echo %VERSION%
)

But there is two problems :

  1. This is getting with loop. isn’t there any better way to get it?
  2. It returns "4.2.4", Which its extra characters needs to be removed, (Clearly i need to have it like 4.2.4

Thx to you all.

Solution

It is generally a good idea to use tools that understand the language when processing a language. If you are on a supported windows system, powershell.exe is available, just as much as findstr.exe is available.

powershell.exe -NoLogo -NoProfile -Command ^
    "(& curl.exe -ks https://api.github.com/repos/pmmp/PocketMine-MP/releases/latest | ConvertFrom-Json).tag_name"

To put this into a batch-file, you could use:

FOR /F "delims=" %%A IN ('powershell.exe -NoLogo -NoProfile -Command ^
    "(& curl.exe -ks https://api.github.com/repos/pmmp/PocketMine-MP/releases/latest | ConvertFrom-Json).tag_name"') DO (SET "TAG_NAME=%%~A")
ECHO TAG_NAME is set to %TAG_NAME%

To use PowerShell commands, you could use:

powershell.exe -NoLogo -NoProfile -Command ^
    (Invoke-RestMethod -Uri https://api.github.com/repos/pmmp/PocketMine-MP/releases/latest).tag_name

Answered By – lit

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *