- @goto start
- :help
- @echo.===== Ver: 1.1 =========================== 发布日期: 2010-09-24 =======
- @echo. BAT_NAME: findDupFile.bat
- @echo. 返回值: %ret_filename%
- @echo. 作用 : 根据MD5值查找重复文件
- @echo. 调用: ①findDupFile.bat [Path [EXT]] [file]
- @echo. ②findDupFile.bat [Path [EXT]] [file] [MD5_file1]
- @echo. ③findDupFile.bat [MD5_file1] [MD5_file2]
- @echo. [Path]: 文件夹路径 (可以是多个)
- @echo. [file]: 文件 (可以是多个)
- @echo. [EXT] : 文件后缀 如:"*.jpg *.bmp",带空格的必须用引号括起来
- @echo. [MD5_file] : MD5文件,形如"*_MD5.txt",带空格的必须用引号括起来
- @echo.====================== Copyright@ by hf-g ========= [彭城] ============
- ping /n 4 127.1>nul
- goto:end
- :start ============================================================
- @echo off&setlocal
- path=%path%;%~d0\DOS\command\第三方命令行程序
- set "ret_filename=%~dp0.\ret_List.txt"
- if /i "%~1"=="" goto help
- if /i "%~1"=="-?" goto help
- set "ext="&set "pathname="&set "Dir_name="&set "MD5_file1="&set "MD5_file2="&set "p_str="
- set /a exit_BL=0,D_count=0,f_count=0
-
- call:get_P %*
- if "%exit_BL%"=="1" goto end
- ::设置临时文件
- set "tmpfname=%pathname_0:~-2%"
- if "%tmpfname%"==":\" (
- set "Dir_name=drive_%pathname_0:~0,1%"
- call echo 驱动器名: %%Dir_name%%
- ) else (
- if "%pathname_0:~-1%"=="\" (
- call call:extpath "%%pathname_0:~0,-1%%"
- call echo 文件夹名: %%Dir_name%%
- ) else (
- if exist "%~1\" (
- set "Dir_name=%~nx1"
- call echo 文件夹名: %%Dir_name%%
- )
- )
- )
- if %f_count% GTR 0 set "Dir_name=list"
- if %D_count% GTR 1 set "Dir_name=list"
-
- set "tmpfname=%~dp0.\%Dir_name%_MD5.txt"
- cd.>"%ret_filename%"
- echo 生成MD5文件: %tmpfname%
- echo 相同文件列表: %ret_filename%
- echo 删除重复文件BAT: del_list.bat
- ::========================MAIN============================
- ::生成MD5文件,可一次处理多个文件夹和文件,只在批处理所在文件夹生成1个MD5文件.
- ::set "ext=*.jpg *.bmp"
- set/a D_count-=1,f_count-=1
- if not "%pathname_0%"=="" (
- cd.>"%tmpfname%"
- for /l %%i in (0,1,%D_count%) do (
- call pushd "%%pathname_%%i%%"
- (for /f "tokens=* delims=" %%i in ('dir /a-d/b/s %ext%') do (md5 "%%i"))>>"%tmpfname%"
- popd
- ))
- if not "%file_0%"=="" (
- if "%pathname_0%"=="" cd.>"%tmpfname%"
- for /l %%i in (0,1,%f_count%) do (call md5 "%%file_%%i%%")>>"%tmpfname%"
- )
- ::处理MD5文件━━━━━━━━━━━━━━━━━━━━━━━━━━━━
- cd.>"%~dp0.\del_list.bat"
- if "%MD5_file1%"=="" (
- rem =========查找上面"生成MD5文件"中相同的文件,删除重复的====
- call:MD5_1file "%tmpfname%"
- ) else (
- if "%MD5_file2%"=="" (
- if not exist "%tmpfname%" (
- rem =========查找原有的唯一MD5列表文件中相同的文件,删除重复的====
- call:MD5_1file "%MD5_file1%"
- ) else (
- rem =========查找上面"生成MD5文件"与原有MD5文件中相同的文件,删除重复的====
- call:MD5_2file "%MD5_file1%" "%tmpfname%"
- )
- ) else (
- rem =========查找原有MD5_file1与原有MD5_file2文件中相同的文件,删除重复的====
- call:MD5_2file "%MD5_file1%" "%MD5_file2%"
- )
- )
-
- :end ==============================================================
- ::if exist "%tmpfname%" del "%tmpfname%" "%~dp0.\del_list.bat"
- exit /b
- ::=========查找MD5列表文件中相同的文件,删除重复的====
- :MD5_1file MD5_file
- for /f "tokens=1* delims= " %%a in ('type "%~1"') do (
- set/p =-<nul
- if defined _MD5_%%a (
- set/p =\<nul
- echo @del /f/q "%%b">>"%~dp0.\del_list.bat"
- set/p =^|<nul
- if not defined _write_%%a (findstr "^%%a" "%~1">>"%ret_filename%"&echo.>>"%ret_filename%"&set "_write_%%a=1")
- ) else (
- set "_MD5_%%a=1"
- )
- set/p =/<nul
- )
- goto:eof
- ::=========查找MD5_file1中与MD5_file2相同的文件,删除MD5_file2中重复的====
- :MD5_2file MD5_file1 MD5_file2
- (echo MD5_file1=%~1&echo MD5_file2=%~2)>con
- (echo MD5_file1=%~1&echo MD5_file2=%~2&echo.)>>"%ret_filename%"
- for /f "tokens=1* delims= " %%a in ('type "%~1"') do (set "_MD5_%%a=%%b")
- for /f "tokens=1* delims= " %%a in ('type "%~2"') do (
- set/p =-<nul
- if defined _MD5_%%a (
- set/p =\<nul
- echo @del /f/q "%%b">>"%~dp0.\del_list.bat"
- set/p =^|<nul
- (set _MD5_%%a&echo %%a %%b&echo.)>>"%ret_filename%"
- set/p =/<nul
- )
- )
- goto:eof
- ::====================================================
- :extpath
- set "Dir_name=%~nx1"
- goto:eof
- ::========================参数设置与获得============================
- :get_p
- ::判断%1是_MD5.txt还是文件夹或其他文件或文件后缀
- if "%~1"=="" goto:eof
- set "p_str=%~1"
- if exist "%p_str%" (
- if "%p_str:~-8%"=="_MD5.txt" (
- if "%MD5_file1%"=="" (
- set "MD5_file1=%p_str%"&shift /1&goto get_p
- ) else (
- set "MD5_file2=%p_str%"&shift /1&goto get_p
- )
- )
- if exist "%p_str%.\" (
- call set "pathname_%%D_count%%=%p_str%"&set /a D_count+=1&shift /1&goto get_p
- )
- call set "file_%%f_count%%=%p_str%"&set /a f_count+=1&shift /1&goto get_p
- ) else (
- if "%ext%"=="" (
- if "%p_str:~0,2%"=="*." (
- set "ext=%p_str%"&shift /1&goto get_p
- )
- )
- echo %p_str% 不是一个正确的文件夹路径或文件^!&set /a exit_BL=1&shift /1&goto end
- )
- @echo off
- :get_p_end
- goto:eof
复制代码 链接: https://pan.baidu.com/s/12tmDkBEy0n-xWmbVNUpcbw?pwd=qix3 |