batch-file - BATCH - 将特定的命令输出行写为没有临时文件的变量?
问题描述
certutil-hash.cmd的代码:
@echo off
certutil -hashfile "%~dpnx0" md5
pause>nul
我想将整个第二行与变量中的哈希值一起保存。CMD-输出:
MD5 hash from C:\Users\ZerTerO\Desktop\certutil-hash.cmd:
9913d66d0b741494962e94ff540a8147
CertUtil: -hashfile command executed successfully.
对我来说,唯一的解决方案是这样的:
@echo off
cls
call:hashfile
set "md5=%md5: =%"
echo.%md5%
pause>nul
exit
:hashfile
for /f "skip=1 tokens=1 delims=" %%a in ('certutil -hashfile "%~dpnx0" md5') do (set "md5=%%a" & goto:eof)
有没有更优雅的解决方案?
我只需要这样做,因为 Windows 7 和 Windows 8 在值之间写入空格:
set "md5=%md5: =%"
提前致谢...
ZerTerO
解决方案
对于使用参数的CertUtil
版本, (请参阅我的评论),您可以更好地处理循环,如下所示:HashAlgorithm
Set "md5="
For /F Delims^= %%G In ('CertUtil -HashFile "%~f0" MD5^|FindStr /VRC:"[^a-f 0-9]"')Do Set "md5=%%G"
或者像这样更健壮:
Set "md5="
For /F Delims^= %%G In ('^""%__APPDIR__%certutil.exe" -HashFile "%~f0" MD5^|"%__APPDIR__%findstr.exe" /VRC:"[^a-f 0-9]"^"')Do @Set "md5=%%G"
使用findstr排除任何包含非字母字符的行a
, b
, c
, d
, e
, 或f
; 数字字符0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 或9
; 或空格字符,在我看来,比跳过第一行然后在处理完第二行后跳出循环更优雅。
至于你的结果变量,我不确定,在这种情况下,我是否会费心使用set "md5=%md5: =%"
专门删除可能的空格,我只是使用 echo.%md5: =%
. 但是,如果我要在剩余的脚本中足够频繁地使用结果变量,我会在标记部分中执行该任务:
@Echo Off
SetLocal EnableExtensions
ClS
Set "algo=MD5"
Call :HashFile "%~f0" %algo%
Echo(%hash%
Pause 1> NUL
GoTo :EOF
:HashFile
Set "hash="
For /F Delims^= %%G In ('^""%__APPDIR__%certutil.exe" -HashFile %1 %2 2^> NUL ^
^| "%__APPDIR__%findstr.exe" /V /R /C:"[^a-f 0-9]"^"') Do Set "hash=%%G"
If Not "%hash: =%" == "%hash%" Set "hash=%hash: =%"
Exit /B
理想情况下,行5
应该是确定操作系统上的certutil版本是否支持两个参数的例程的一部分。如果不是,那一行将显示为Set "algo="
. 您还会注意到我已将您的硬编码文件名移出循环,并将其用作Call
命令的第一个输入参数。这应该使您的脚本更加模块化,并且 IMO 更优雅。我还认为%hash%
在要求将它们替换为空之前检查是否包含空格会更优雅。优雅和效率不一定是一样的。
推荐阅读
- python - 我得到了这个形状 (1,254,254,1) 而预期的形状是 (1,254,254,3)
- java - Spark 独立模式下工作节点上的线程“主”java.lang.reflect.UndeclaredThrowableException 中的异常
- javascript - 总是承诺,但没有结果
- arrays - 如果 struct 中的字符大小为 30 或更大,则程序终止
- groovy - 对 JSON 响应中的数据进行排序以进行比较
- postgresql - PostgreSQL 没有账户名和安全 ID 之间的映射
- r - 从r中的fasta标头多次提取基因名称
- c - 从 Windows 内核驱动程序中的线程调用驱动程序内存函数
- angular - Angular 上的 Mapbox GL JS MarkerClusterGroup
- java - 在数组的线性搜索中查找相同值的多次出现