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 :
- This is getting with loop. isn't there any better way to get it?
- It returns
"4.2.4",
Which its extra characters needs to be removed, (Clearly i need to have it like4.2.4
Thx to you all.
CodePudding user response:
Here's one option:
@For /F Tokens^=4^ Delims^=^" %%G In ('%SystemRoot%\System32\curl.exe -ks
"https://api.github.com/repos/pmmp/PocketMine-MP/releases/latest" ^|
%SystemRoot%\System32\findstr.exe /IR "\"tag_name\":" 2^>NUL') Do @Echo(%%G
You could of course use your own code and fix it by reading the usage information for the command you are using. You could do that by opening a Command Prompt window, typing for /?
and pressing the ENTER key. Simply changing your delimiters to include the additional whitespace and comma characters, and modifying your variable expansion to use a tilde, should work for you.
CodePudding user response:
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%