извлечение изменяющегося и неизвестного хеш-кода из текстового документа в пакете - PullRequest
0 голосов
/ 26 марта 2020

Я хотел написать небольшой инструмент обновления для файла server.jar, чтобы избавить меня от всех усилий, затрачиваемых на ручную работу. Используемая программа создает папку, в которой содержится файл при выпуске нового обновления. Примерно так работает программа:

get hash of current server.jar
find latest created folder (eg. update12)
open the file in the update12 folder (eg. update12.txt)
***extract the hash code -> %newHash%***    thats where im having the issue
compare the extracted hashcode (%newHash%) and the calculated hashcode (%currentHash%)
if they are not equal go to www.downloadupdate.com/updates/%newHash%/server.jar
download the file and replace the old server.jar with the new one.

У меня проблема в том, что я не могу прочитать ha sh в файле update12.txt, длина файла составляет около 36000 символов и в одну строку только

"server": {"sha1": "16f18c21286a3f566d3d0431d13aa133bebe6eff"

я хочу обнаружить часть "server": {"sha1": "и после этого скопировать ha sh я перепробовал много вариантов, которые нашел на этом форуме, но ни один из них не работал так, как этот

set /p txt=<update12.txt
pause >nul
echo %txt%
pause >nul
for /f %%i in ('powershell -noprofile -c "('%txt:"=\"%' -split '\""')[1]"') do set id=%%i
echo %id%

просто вызвал мгновенное закрытие командного файла, кроме того, установите / p txt =

, если у вас есть какое-либо решение, я бы будь рад услышать это

1 Ответ

0 голосов
/ 27 марта 2020
@echo off


Setlocal EnableDelayedExpansion
xcopy %appdata%\.minecraft\versions\%MCversion%\%MCversion%.json %appdata%\.minecraft\versions\%MCversion%\%MCversion%.text*
ren %appdata%\.minecraft\versions\%MCversion%\%MCversion%.text %MCversion%.txt

set search1= 
set search2=,
set search3=:
set textFile=%appdata%\.minecraft\versions\%MCversion%\%MCversion%.txt

:PowerShell
SET PSScript=%temp%\~tmpStrRplc.ps1
ECHO (Get-Content "%textFile%").replace("%search1%", "") ^| Set-Content "%textFile%">"%PSScript%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
SET PSScript=%temp%\~tmpStrRplc.ps1
echo done.

ECHO (Get-Content "%textFile%").replace("%search2%", "") ^| Set-Content "%textFile%">"%PSScript%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
SET PSScript=%temp%\~tmpStrRplc.ps1
echo done.

ECHO (Get-Content "%textFile%").replace("%search3%", "") ^| Set-Content "%textFile%">"%PSScript%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
SET PSScript=%temp%\~tmpStrRplc.ps1
echo done.
:skip
ECHO (Get-Content "%textFile%").replace("server", "`r`nlookup") ^| Set-Content "%textFile%">"%PSScript%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
echo done.

for /f %%a in ('findstr "lookup\"{\"sha1\"\"" %textFile%') do set "newVersion=%%a"
set newVersion=%newVersion:~15,40%
echo %newVersion%
del %textFile%
pause
:exit
exit

этот код работает для меня, что он делает:

copy the "update12.txt"
look for " " "," and ":" in the copied txt and delete the symbols
look for server replace it with a "*linebreak*lookup" 
find the string lookup"{"sha1""
trim the edges of the string with 15,40
done.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...