根据MD5哈希删除文件

时间:2018-11-27 21:22:57

标签: hash cmd

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

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

非常感谢您的帮助,谢谢!

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

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

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

2 个答案:

答案 0 :(得分:0)

对于cmd行中的单个md5密钥,操作非常简单:
(此处使用的是64位版本)。

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

仅测试echo命令。
在批处理文件中,将%百分号%%A/%%B的倍数翻倍

答案 1 :(得分:0)

此文件使用纯Bath文件,请使用Windows中的 CertUtil ,因此不需要第3 个软件。

您只需要提供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"

-全部语言: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_db\date_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_: