【问题标题】:extracting a changing and unknown hashcode from text document in batch批量从文本文档中提取变化的未知哈希码
【发布时间】:2020-07-07 08:51:59
【问题描述】:

我想为 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.

我遇到的问题是我无法读取 update12.txt 中的哈希值,该文件的长度约为 36000 个符号,并且仅在一行中

“服务器”:{“sha1”:“16f18c21286a3f566d3d0431d13aa133bebe6eff”

我想检测 "server": {"sha1": " 部分并在此之后复制哈希 我尝试了在这个论坛上找到的许多选项,但似乎没有一个有效 喜欢这个

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=

如果您有任何解决方案,我将不胜感激

【问题讨论】:

  • 您是从控制台窗口还是从资源管理器运行批处理文件?尝试将pause 放在末尾或从控制台窗口运行它,以便查看输出。
  • 您可能超过了 maximum command line length 和/或最大环境变量长度 (32K)。
  • 我会在 powershell 中重写整个脚本。
  • 您可能正在尝试读取使用 LF 而不是 CRLF 行结尾的文件。当它实际上是多条线时,它可能显示为一条线。你能确认一下吗?

标签: batch-file batch-processing


【解决方案1】:
@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.

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多