gpt4 book ai didi

windows - 在 CMD 中使用 findstr 从文件中获取 MD5 值

转载 作者:可可西里 更新时间:2023-11-01 11:16:44 25 4
gpt4 key购买 nike

我有以下文件内容,我想从中提取第 2 行的 MD5 哈希值。

如何在 Windows 批处理文件中执行此操作?不幸的是,使用 powerscript 不是一种选择。

这里是输入文件(file.txt)

MD5hashoffile20160613190010_Address
f4f855c5cb40767a7227b506f641ceef
CertUtil:-hashfilecommandcompletedsuccessfully.

我想使用 findstr 实用程序,但我使用的正则表达式一定是错误的,因为它没有返回任何内容。

findstr /R "[a-fA-F0-9]{32}" file.txt

我很感激任何建议。

谢谢

更新:

我已经在答案部分添加了完整的解决方案。

最佳答案

经过反复试验,我找到了一个可行的解决方案:

findstr /R "^[a-fA-F0-9]*$" file.txt

包含完整解决方案的更新答案:

SETLOCAL ENABLEDELAYEDEXPANSION

SET "FILESIZE=0"
SET "RECORDCOUNT=0"
SET "MD5HASH="
SET "TEMPHASH="
SET "FILESETREPORT=_UNSET"
SET "LINE=0"
SET "COLUMNS=TableName|RecordCount|FileSize|FileHash"

echo "============================="
echo "= Starting FileSize process ="
echo "============================="

for %%F in (*.csv) do (
echo "Process A CSV File"

for /f "tokens=1,2,3,4 delims=^_" %%A IN ("%%~nF") do (
SET "TABLENAME=%%D"
SET "FILESIZE=%%~zF"
if "!FILESETREPORT!" == "_UNSET" (
SET "FILESETREPORT=%%A_%%B_%%C_FilesetReport.csv"
:: Initialize the headers
echo !COLUMNS! > !FILESETREPORT!
)
)
echo "Get RecordCount in CSV"
if "!TABLENAME!" NEQ "FilesetReport" (
for /f "tokens=*" %%I in ('more +1 %%F ^| find /v /c ""') do SET "RECORDCOUNT=%%I"
echo "Generate File MD5 Hash"
certUtil -hashfile %%~nxF MD5 > hashfile
echo "Get the hash from the file"
for /f "skip=1 tokens=*" %%A in (hashfile) do (
SET "TEMPHASH=%%A"
:: call :cleanHash
if !LINE! == 0 SET "LINE=1" && call :cleanHash
)

:: Reset the Line Number
SET "LINE=0"

echo "Save File Details to FieldsetReport"
echo "RecordCount : !RECORDCOUNT!"
echo "FileSize : !FILESIZE!"
echo "MD5Hash : !MD5HASH!"
echo "TableName : !TABLENAME!"
SET "OUTPUTLINE=!TABLENAME!|!RECORDCOUNT!|!FILESIZE!|!MD5HASH!"
echo !OUTPUTLINE! >> !FILESETREPORT!

:: Cleanup
del hashfile
)
)

echo "File Processing Completed"
exit /b 0

:cleanHash
echo "Remove all spaces from the MD5 hash"
:: for /f "delims=" %%a in ('findstr /R "^[a-fA-F0-9]*$" !TEMPHASH!') do SET TEMPHASH=%%a
SET "MD5HASH=!TEMPHASH: =!"
echo "********************************************************************"
echo !MD5HASH!

ENDLOCAL

关于windows - 在 CMD 中使用 findstr 从文件中获取 MD5 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com