我使用 curl 下載了一個檔案,我正在努力讓批處理找到 x 的最高值“paper-1.19.2-x”。我寫了這段代碼`
@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
我下載檔案并將其命名為 builds.txt,然后嘗試讀取檔案并查找最高的 x 值。但是這個輸出很簡單
paper-1.19.2-.jar
我認為我做錯的是沒有正確讀取檔案。有什么幫助嗎?
編輯
curl 下載的 builds.txt 只是一個我轉換成 txt 的 json 檔案,所以它基本上轉換為 1 行 txt 檔案,其中的部分代碼看起來像這樣
`
{"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"
新的批處理代碼是`
@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%
它只是把這個`
05T23:08:28
uj5u.com熱心網友回復:
有一種更簡單的方法可以為您的資料獲得相同的結果:
@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
輸出:
paper-1.19.2-256.jar
uj5u.com熱心網友回復:
您build.txt
不包含任何行尾,因此它太大而無法由純批處理腳本處理。
您需要一種搜索??檔案的方法(最好使用 REGEX)。Powershell 可能會提供一個優雅的解決方案,但如果您被 cmd/batch 困住,您可以下載jrepl.bat(由dbenham撰寫,該社區的知名成員),它能夠批量使用完整的 REGEX:
for /f "delims=" %a in ('^<build.txt jrepl "paper-1.19.2-\d*" "" /match') do set "lastMatch=%%a"
echo %lastMatch%.jar
REGEX 模式"paper-1.19.2-\d*"
查找"paper-1.19.2-
后跟任意位數的任何字串(""
不使用引數(“replacement”),但出于語法原因必須存在)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/529339.html
標籤:批处理文件
上一篇:使用批處理編輯txt檔案中的內容