根据MD5哈希删除文件 [英] Delete files based on MD5 hash

查看:70
本文介绍了根据MD5哈希删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Windows command line (not powershell), I want to hash all files within the directory and then remove files that match a particular hash set contained within a text file.

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

I've considered using md5deep, but I'm unsure if the output of matched files can then be redirected into a delete command.

任何帮助表示感谢,谢谢!

Any help gratefully received, thank you!

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

To add some detail; the files are in a directory called 'images'

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

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

This gives me a list of the files that I need to keep. Is it possible to redirect the output from MD5deep to move the 'good' files to another directory?

推荐答案

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

This use pure bath file, take use of CertUtil from windows, so no 3rd software need.

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

You only need provide the value for 2 variables:

设置_nOp = irrelevant_hashes.txt ::具有md5数据的文件文本以删除文件

set _nOp=irrelevant_hashes.txt :: file text with md5 data for files to delete

设置"_path_gen = c:\ images" ::将要删除的文件的路径.

set "_path_gen=c:\images" :: path to files that will be delete.

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

In additional, put the txt file with md5 (in this code case irrelevant_hashes.txt) in the same folder of the files that you want to delete.

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

For generate a new md5 from files, and usin certUtil on command line:

   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 :

For generate a new md5 from files, and using certUtil on a file.bat:

   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"


@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_:

这篇关于根据MD5哈希删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆