![](/img/trans.png)
[英]Windows batch file to list all duplicates (and the original file) in tree and sort them
[英]Windows batch file to find duplicates in a tree
我需要一个批处理文件(Windows CMD是解释器,一个.bat)来执行这种类型的任务:
1)搜索文件夹及其子文件夹
2)查找具有相同文件名和扩展名的文件(也称为重复文件)
3)检查它们是否具有相同的尺寸
4)如果同名+相同大小,则回显除第一个以外的所有文件(实际上我需要删除除一个副本以外的所有文件)
感谢您提供的任何帮助
这只是一个初始脚本,仅用于检查文件,文件夹及其子文件夹中的文件及其大小:
@Echo off
Setlocal EnableDelayedExpansion
Set Dir=C:\NewFolder
For /r "%Dir%" %%i in (*) do (
Set FileName=%%~nxi
Set FullPath=%%i
Set Size=%%~zi
Echo "!FullPath!" - SIZE: !Size!
)
Echo.
Pause
这个脚本做你要求的。 只需在顶部设置ROOT变量即可指向树的根。
@echo off
setlocal disableDelayedExpansion
set root="c:\test"
set "prevTest=none"
set "prevFile=none"
for /f "tokens=1-3 delims=:" %%A in (
'"(for /r "%root%" %%F in (*) do @echo %%~znxF:%%~fF:)|sort"'
) do (
set "currTest=%%A"
set "currFile=%%B:%%C"
setlocal enableDelayedExpansion
if !currTest! equ !prevTest! echo "!currFile!"
endlocal
set "prevTest=%%A"
)
但是,通过使用FC来比较文件的内容,可以使测试更加精确。 此外,您可以直接在脚本中合并DEL命令。 下面的脚本打印出删除重复文件的命令。 当您准备好实际删除文件时,在DEL命令之前删除ECHO。
@echo off
setlocal disableDelayedExpansion
set root="c:\test"
set "prevTest=none"
set "prevFile=none"
for /f "tokens=1-3 delims=:" %%A in (
'"(for /r "%root%" %%F in (*) do @echo %%~znxF:%%~fF:)|sort"'
) do (
set "currTest=%%A"
set "currFile=%%B:%%C"
setlocal enableDelayedExpansion
set "match="
if !currTest! equ !prevTest! fc /b "!prevFile!" "!currFile!" >nul && set match=1
if defined match (
echo del "!currFile!"
endlocal
) else (
endlocal
set "prevTest=%%A"
set "prevFile=%%B:%%C"
)
)
这两组代码看起来都过于复杂,但这只是因为我将代码结构化为健壮并避免了可能困扰简单解决方案的问题。 例如, !
如果启用了延迟扩展,文件名中的文件名可能会导致FOR变量出现问题,而文件名中的=
会导致npocmoka解决方案出现问题。
@echo off
setlocal
for /f "tokens=1 delims==" %%# in ('set _') do (
set "%%#="
)
for /r %%a in (*.*) do (
if not defined _%%~nxa%%~za (
set "_%%~nxa%%~za=%%~fa"
) else (
echo %%~fa
)
)
endlocal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.