Search code examples
batch-filebatch-processing

extracting a changing and unknown hashcode from text document in batch


i wanted to code a little update tool for a server.jar file to save me all the effort of doing it manually the program i'm using creates a folder which contains a file when a new update is released. this is pretty much how the program works:

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.

the issue i am having is that i cant read out the hash in the update12.txt the file is about 36000 symbols long and in one line only

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

i want to detect the "server": {"sha1": " part and copy the hash after that i tried many options i found on this forum but none seemed to work like this one

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%

just caused the batch file to close instantly furthermore set /p txt=

if you have any solution i would be grateful to hear it


Solution

  • @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
    

    this code works for me, what it does:

    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.