使用BATCH定义路径中文件的哈希文件

时间:2019-03-19 14:28:26

标签: batch-file

我正在尝试获取外部文件PathList.txt中的子文件夹中所有文件的MD5值。 我无法使脚本使用%%i中定义的路径名。
在示例中,我使用"C:\Temp\example\"代替了%%i,因此脚本可以正常工作。 此外,它还会跳过带有C:\folder2\some file.txt

等空格的文件
@ECHO off
echo Files MD5 > MD5_log.txt

FOR /F %%i IN (PathList.txt) DO (
    @FOR /R "C:\Temp\example\" %%G in (*) DO (
     for  %%a in (%%G) do certutil -hashfile "%%~a" MD5 | find /i /v "certutil">> MD5_log.txt
     )
)

PathList.txt

C:\folder1\
C:\folder2\

这是用于旧计算机,因为我没有Powershell选项。

我如何使其起作用? 谢谢

2 个答案:

答案 0 :(得分:1)

要具有类似于常规工具md5deep64.exe,MD5SUMS.EXE等的输出,
并将完整路径包括在与哈希相同的行上
我建议按照以下方式使用一些东西:

:: Q:\Test\2019\03\19\SO_55243349.cmd
@ECHO off
( echo Files MD5 
  FOR /F "delims=" %%P IN (.\PathList.txt) DO if exist "%%~fP" (
    PushD %%P
       FOR /R %%R in (*) DO (
          FOR /F %%A in ('certutil -hashfile "%%~R" MD5 ^| find /i /v ":" ') Do Echo %%A  %%~fR
       )
    PopD
  ) else ( Echo %%~fP not found )
) > MD5_log.txt

示例输出:

> Q:\Test\2019\03\19\SO_55243349.cmd
Files MD5
bea07e6d2b8dce396fe21baa61b34956  A:\a\dmc\foo.txt
81051bcc2cf1bedf378224b0a93e2877  A:\a\dmc\C24117\bar.txt
81051bcc2cf1bedf378224b0a93e2877  A:\b\dmc\bar.txt

答案 1 :(得分:0)

我无法使脚本使用%%i

中定义的路径名

请在下面找到一个有效的批处理文件(test.cmd):

@echo off
setlocal EnableDelayedExpansion
echo Files MD5 > MD5_log.txt
for /f "tokens=*" %%i in (PathList.txt) do (
  for /f "tokens=*" %%j in ('dir /b /s "%%i"') do (
    certutil -hashfile "%%j" MD5 | find /i /v "certutil" >> MD5_log.txt
    )
  )
endlocal

示例输出:

> type PathList.txt
f:\test\bar
f:\test\bar - Copy
f:\test\foo

> test
> type MD5_log.txt
Files MD5
MD5 hash of file f:\test\bar\test.cmd:
eb 4f 28 f4 a0 b0 c5 21 0d e8 5f 99 0f d8 fd ab
MD5 hash of file f:\test\bar\test.html:
3a 68 3a f6 4e 88 f1 22 62 d6 46 dc bb 54 59 45
MD5 hash of file f:\test\bar\test.ps1:
07 fd 41 59 6b fa 90 06 49 4f bf e3 dd be 0d 1c
MD5 hash of file f:\test\bar - Copy\test with space.cmd:
eb 4f 28 f4 a0 b0 c5 21 0d e8 5f 99 0f d8 fd ab
MD5 hash of file f:\test\bar - Copy\test.html:
3a 68 3a f6 4e 88 f1 22 62 d6 46 dc bb 54 59 45
MD5 hash of file f:\test\bar - Copy\test.ps1:
07 fd 41 59 6b fa 90 06 49 4f bf e3 dd be 0d 1c
MD5 hash of file f:\test\foo\test.sh:
d2 12 38 76 9d 8e 9f 51 1a 60 0b 15 6c 0c f8 38
MD5 hash of file f:\test\foo\test.xml:
cd 8f d1 c6 66 ac ff 7f 98 d2 e9 4a ad b5 20 1f
MD5 hash of file f:\test\foo\test.yaml:
78 ce a1 f0 97 46 ee 32 c6 7f f6 16 8d 94 04 d2
MD5 hash of file f:\test\foo\test.yml:
78 ce a1 f0 97 46 ee 32 c6 7f f6 16 8d 94 04 d2

进一步阅读