删除基于MD5哈希的文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除基于MD5哈希的文件相关的知识,希望对你有一定的参考价值。

使用Windows命令行(而不是powershell),我想散列目录中的所有文件,然后删除与文本文件中包含的特定哈希集匹配的文件。

我考虑过使用md5deep,但我不确定匹配文件的输出是否可以重定向到删除命令。

感谢任何帮助,谢谢!

添加一些细节;这些文件位于名为“images”的目录中

md5deep.exe -r -b -x irrelevant_hashes.txt c:images

这给了我一个我需要保留的文件列表。是否可以重定向MD5deep的输出以将“好”文件移动到另一个目录?

答案

对于cmd行中的单个md5键,它非常简单: (这里使用64位变体)。

set "KillMD5=00112233445566778899aabbccddeeff"
for /f "tokens=1*" %A in ('md5deep64.exe *.* 2^>mul') do @if %A==%KillMD5% @Echo del "%B"

为了测试del命令只是回显。 在批处理文件中,%百分号加倍%%A/%%B

另一答案

这使用纯浴文件,从windows使用CertUtil,因此没有第三软件需要。

您只需要提供2个变量的值:

set _nOp=irrelevant_hashes.txt ::文件文本,包含要删除的文件的md5数据

set "_path_gen=c:images" ::将被删除的文件的路径。

另外,将带有md5的txt文件(在此代码中为irrelevant_hashes.txt)放在要删除的文件的同一文件夹中。

要从文件生成新的md5,并在命令行上使用certUtil

   type nul >"%temp%MD5_to_del.txt" & cd /d "c:images" & for /f "tokens=* delims= " %i in ('^<nul dir /o-d /on /b "c:images*.*"') do for /f "tokens=* delims= " %M in ('certutil -hashfile "%i" md5 ^| find /v ":"') do echo/%M>>"%temp%MD5_to_del.txt"

要从文件生成新的md5,并在file.bat上使用certUtil

   type nul >"%temp%MD5_to_del.txt" & cd /d "c:images" & for /f "tokens=* delims= " %%i in ('^<nul dir /o-d /on /b "c:images*.*"') do for /f "tokens=* delims= " %%M in ('certutil -hashfile "%%i" md5 ^| find /v ":"') do echo/%%M>>"%temp%MD5_to_del.txt"

- language-all:lang-bat - >

@echo off & setlocal EnableExtensions EnableDelayedExpansion 

cls && mode con cols=120 lines=7 & cd /d "%~dp0" 

set "'=^|"
set "_spc=            "

set /a _cnt_left= 1 - 1

set /a _cnt_treated= 1 - 1
set /a _cnt_deleted= 1 - 1
set /a _cnt_keeped_= 1 - 1

set _type_hash=md5

set _show=0000000

set "_path_gen=c:images"

cd /d !_path_gen! || set "_erro=!_path_gen! not exist?"&& goto :_error_:

set _n_deleted=%temp%md5_not_deleted_.txt
set _y_deleted=%temp%md5_was_deleted_.txt

(

if exist "!_n_deleted!" del /q /f "!_n_deleted!"
if exist "!_y_deleted!" del /q /f "!_y_deleted!"

) 2>nul >nul

if exist ".irrelevant_hashes.txt" ( 

     set _nOp=irrelevant_hashes.txt

    ) else ( 

     set "_error= File irrelevant_hashes.txt not found"
     goto :_error_:

    )

set _hash_data=%temp%hash_dbdate_db.txt

if exist "!_hash_data!" ( 

     del /q /f "!_hash_data!" 
     copy /y "!_nOp!" "!_hash_data!" 2>nul >nul || set _error=Copy !_nOp! to !_hash_data!

    ) else (

     dir /ad "%temp%hash_db" 2>nul >nul | findstr /c:".." || mkdir "%temp%hash_db" 
     copy  /v "!_nOp!" "!_hash_data!" 2>nul >nul || set _error=Copy !_nOp! to !_hash_data!

    )

for /f "delims= " %%T in ('forFiles /p "." /m "%~nx0" /c "cmd /c echo(0x09"') do set "_tab=%%T"

call set "_tab=%_tab:~0,1%"

for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "!_path_gen!*.*" 2^> nul ^| find "" /v /c ') do set _cnt_file=%%i

for /f "tokens=* delims= " %%f in ('dir /o-d /on /b "*.*" ^| findstr /i /v "!_nOp! %0" ') do (

     for %%S in (cnt_treated cnt_deleted cnt_keeped_ cnt_left) do set _show_%%S=!_show!!_%%S!

     set _file=%%~nxf
     set "_file_hash=%%~dpnxf"

     set /a _cnt_treated=!_cnt_treated! + 1 

     call :_get_hash_:

     title Total files: !_show_cnt_treated:~-5! ^| Delete: !_show_cnt_deleted:~-5! ^| Keeped: !_show_cnt_keeped_:~-5! ^| File left: !_show_cnt_left:~-5!

    )

(

if exist "!_n_deleted!" copy /y "!_n_deleted!" .
if exist "!_y_deleted!" del /y "!_y_deleted!" .

) 2>nul >nul

echo/ Total file treated: !_show_cnt_treated:~-5! 
echo/ Total file deleted: !_show_cnt_deleted:~-5!
echo/ Total file  keeped: !_show_cnt_keeped_:~-5!

goto :_end_of_file_:

:_get_hash_:

for /f "tokens=* delims= " %%i in ('certutil -hashfile "!_file_hash!" !_type_hash! ^| find /v ":"') do (

          set "_hash=%%i"
     call set "_hash=!_hash: =!"

     call set _hash=!_hash!

     for /f "tokens=* delims=" %%I in ('echo/%_tab%') do call set _file_hash=!_hash:%%I=!

     call set _hash=!_hash!

     for /f "tokens=* delims= " %%e in ('type "!_hash_data!" ^| findstr /i /l /c:"!_hash!"') do set _hash_db=%%e

     if "!_hash_db!" equ ""  set "_hash_db=-  So, no found^!! not "

     echo/ & echo/  File hash md5 from: "!_file!" & echo/
     echo/!_spc!Hash !_type_hash!: !_hash!
     echo/ Data hash md5 ^(db^): !_hash_db! equals?

     call :_delete_md5_:

     call set /a _cnt_left=!_cnt_file! - 1 - !_cnt_keeped_!

    )

    timeout /t 10 2>nul >nul 

   )

   )

for %%C in (file_hash hash strn_hash strn_hash hash_nul hash_db) do set _%%C=

exit /b 

:_delete_md5_:

if /i "!_hash_db!" equ "!_hash!" del /q /f "!_path_gen!!_file!"

if not exist "!_path_gen!!_file!" (

     echo/!_file! !_strn_hash!>>"!_y_deleted!" 
     echo/!_spc!!_spc:~-10!!_hash! "!_file!" file was deleted^^! 

     call set /a _cnt_file=!_cnt_file! - 1
     call set /a _cnt_deleted=!_cnt_deleted! + 1

     exit /b

     ) else (

     echo/!_file! !_strn_hash!>>"!_n_deleted!" 
     echo/!_spc!!_spc:~-10!!_hash! "!_file!" file not was deleted^^!

     call set /a _cnt_keeped_=!_cnt_keeped_! + 1

     exit /b

    )

:_error_:

echo/ & echo/ & echo/ !_error! 

goto :_end_of_file_:


:_end_of_file_:

以上是关于删除基于MD5哈希的文件的主要内容,如果未能解决你的问题,请参考以下文章

获取python中带有子目录的目录中的所有文件md5哈希

如何使用 VBA 获取文件的 MD5 十六进制哈希?

如何在 JavaScript/Node.js 中生成 MD5 文件哈希?

哈希函数的常用构造方法

批处理命令怎么校验文件MD5哈希值?

可以安全地重用 MD5CryptoServiceProvider 来计算多线程代码中的 md5 哈希吗?