Home > database >  How do I look for the highest value of something in a txt file using Batch and set it as a value?
How do I look for the highest value of something in a txt file using Batch and set it as a value?

Time:11-04

I downloaded a file using curl and I am tyring to make batch find the highest value "paper-1.19.2-x" of x. I wrote this code `

@ECHO off
curl "https://api.papermc.io/v2/projects/paper/versions/1.19.2/builds/" --output builds.txt
setlocal enabledelayedexpansion
For /f %%i in (builds.txt) do (
    for %%x in (paper-1.19.2-*.jar) do (
         set "line=%%~nx"
         set "line=!FN:paper-1.19.2-=!"
         if !line! GTR !max! set max=!line!
))
Echo paper-1.19.2-%max%.jar

I download the file and name it builds.txt then I try to read the file and look for the highest x value. but the output of this is simply paper-1.19.2-.jar

I think what I am doing incorrectly is not reading the file correctly. Any help?

edit

the builds.txt that is downloaded by curl is just a json file that i convert into txt, so it basically converts to 1 line txt file and part of the code from it looks like this

`

{"project_id":"paper","project_name":"Paper","version":"1.19.2","builds":[{"build":112,"time":"2022-08-05T23:08:28.926Z","channel":"default","promoted":false,"changes":[{"commit":"bef2c9d005bdd039f188ee53094a928e76bd8e59","summary":"1.19.2 (#8250)","message":"1.19.2 (#8250)\n\n"}],"downloads":{"application":{"name":"paper-1.19.2-112.jar","sha256":"59e5b07dbffcbceeef15ebbe77ffcc8a56f58fdfcb2d3092be256c32a5c9588d"},"mojang-mappings":{"name":"paper-mojmap-1.19.2-112.jar","sha256":"f743f109522b2a21b290e9e9e8015e2e630ec74d6e7496a6bc31e5af34f2a4bd"}}},{"build":113,"time":"2022-08-06T23:30:43.315Z","channel":"default","promoted":false,"changes":[{"commit":"a15152e96a0c1f8b8f6792f4308e8077e01614d2"

the new batch code is `

@ECHO off

setlocal EnableExtensions 
setlocal EnableDelayedExpansion
curl "https://api.papermc.io/v2/projects/paper/versions/1.19.2/builds/" --output builds.txt
set "MaxNumber=0"
if exist "builds.txt" for /F "tokens=5 delims=-." %%I in ('%SystemRoot%\System32\findstr.exe /R "paper-.*\.jar" "builds.txt"') do if %%I GTR !MaxNumber! set "MaxNumber=%%I"
Echo %MaxNumber%

and it just out puts this `

05T23:08:28

CodePudding user response:

Your build.txt doesn't contain any line endings, so it's too big to be processed by a pure batch script.

You need a way to search the file (at best with REGEX). Powershell may provide an elegant solution, but if you are stuck with cmd/batch, you can download jrepl.bat (written by dbenham, a highly reputable member of this community), which is able to use full REGEX in batch:

for /f "delims=" %a in ('^<build.txt jrepl "paper-1.19.2-\d*" "" /match') do set "lastMatch=%%a"
echo %lastMatch%.jar

The REGEX pattern "paper-1.19.2-\d*" looks for any string "paper-1.19.2- followed by any number of digits (the parameter "" ("replacement") is not used, but has to be present for syntax reasons)

CodePudding user response:

There is a much simpler way to get the same result for your data:

@echo off
setlocal EnableDelayedExpansion

curl "https://api.papermc.io/v2/projects/paper/versions/1.19.2/builds/" --output builds.txt
call :readFile < builds.txt
for /F "delims=." %%a in ("!last:*paper-1.19.2-=!") do echo paper-1.19.2-%%a.jar
goto :EOF

:readFile
set "last=%line%"
set /P "line="
if not errorlevel 1 goto readFile
exit /B

Output:

paper-1.19.2-256.jar
  • Related