Delete files based on MD5 hash

感情迁移 提交于 2021-01-29 14:33:05

问题


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.

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!

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

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

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?


回答1:


For a single md5 key in cmd line it is as easy as:
(Here using the 64bit variant).

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

For testing the del command is only echoed.
In a batch file double the % percent signs %%A/%%B




回答2:


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

You only need provide the value for 2 variables:

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

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

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.

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"

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



来源:https://stackoverflow.com/questions/53508344/delete-files-based-on-md5-hash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!