FINDSTR始终无法找到在文本文件哈希串 [英] Findstr always fails to find hash string in text file

查看:144
本文介绍了FINDSTR始终无法找到在文本文件哈希串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图取代某个.jar文件如果文件更改MD5哈希值。我写了一个小PowerShell脚本做文件的散列和的.ps1脚本通过批处理文件运行。

在PowerShell中打印成散列的1.txt 我要批处理脚本来检查文本文件正确的哈希值,如果哈希不同,它会覆盖该文件与旧版本。该文件尚未实现的更换,但将是一次 FINDSTR 问题解决了。

关闭@echo
SETLOCAL EnableDelayedExpansion:一个
powershell.exe -ExecutionPolicy旁路-file powershellmd5.ps1FINDSTR / C:FF B1 B9 ​​2D B103分贝59 3F 9E CA 51 F0 14 67 62 CA A8 D7 7D的1.txt
回音!错误级别!
超时/吨10 / NOBREAK
转到一个

下面是内容的1.txt 当散列齐全:

文件license.jar的SHA1哈希:
FF B1 B9 ​​2D B103分贝59 3F 9E CA 51 F0 14 67 62 CA A8 D7 7D
CERTUTIL:-hashfile命令成功完成

的错误级别始终为1,即使串是相同的一个在文本文件中。也许我现在用的参数不对?

我用出文件 powershellmd5.ps1 写的结果是:

的certutil -hashfile LICENSE.TXT | OUT-文件1.txt的


解决方案

显然,你使用的 出文件 创建的1.txt 。该cmdlet使用统一code(小端UTF-16)作为默认编码, FINDSTR 不支持。该命令处理文件作为ASCII¹文本,因此无法找到匹配。

有处理这个问题的方法有两种:


  • 的1.txt 使用ASCII编码,通过调用出文件 -Encoding 参数:

     的certutil -hashfile LICENSE.TXT |出文件1.txt的-Encoding Ascii码

    通过调用 设置内容或$ C> (默认为ASCII编码):

     的certutil -hashfile LICENSE.TXT |设置内容的1.txt


  • 使用 找到 命令(支持单向code ),而不是 FINDSTR

     找到FF B1 B9 ​​2D B103分贝59 3F 9E CA 51 F0 14 67 62 CA A8 D7 7D的1.txt



¹是的,我知道,它实际上是一个 ANSI编码的,但参数变量命名为 ASCII ,让我们坚持使用该名称上,以免混淆。

I'm trying to replace a certain .jar file if the MD5 hash of the file changes. I've written a small PowerShell script to do the hashing of the file and the .ps1 script is run through a batch file.

After PowerShell prints the hash into 1.txt I want the batch script to check the text file for the correct hash and if the hash is different it will overwrite the file with the old version. The replacing of the file is not yet implemented but will be once the findstr issue is resolved.

@echo off
setlocal EnableDelayedExpansion

:a
powershell.exe -ExecutionPolicy ByPass -file powershellmd5.ps1

findstr /c:"ff b1 b9 2d b1 03 db 59 3f 9e ca 51 f0 14 67 62 ca a8 d7 7d" "1.txt"
echo !errorlevel!
timeout /t 10 /NOBREAK
goto a

Here is the content of 1.txt when the hashing is complete:

SHA1 hash of file license.jar:
ff b1 b9 2d b1 03 db 59 3f 9e ca 51 f0 14 67 62 ca a8 d7 7d
CertUtil: -hashfile command completed successfully.

The errorlevel is always 1, even though the string is identical to the one in the text file. Maybe I am using the arguments wrong?

I'm using Out-File in powershellmd5.ps1 to write the result:

certutil -hashfile license.txt | Out-File 1.txt

解决方案

Apparently you're using Out-File for creating 1.txt. The cmdlet uses Unicode (little endian UTF-16) as the default encoding, which findstr doesn't support. The command processes the file as ASCII¹ text and thus can't find a match.

There are two ways of approaching this problem:

  • Write 1.txt using ASCII encoding, either by calling Out-File with the -Encoding parameter:

    certutil -hashfile license.txt | Out-File 1.txt -Encoding Ascii
    

    or by calling Set-Content (which defaults to ASCII encoding):

    certutil -hashfile license.txt | Set-Content 1.txt
    

  • Use the find command (which supports Unicode) instead of findstr:

    find "ff b1 b9 2d b1 03 db 59 3f 9e ca 51 f0 14 67 62 ca a8 d7 7d" 1.txt
    


¹ Yes, I know, it's actually an ANSI encoding, but the parameter argument is named Ascii, so let's stick with that name for now to avoid confusion.

这篇关于FINDSTR始终无法找到在文本文件哈希串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆