批处理脚本以递归方式循环子目录并在2个文件夹中查找丢失的文件

时间:2013-07-24 07:43:05

标签: batch-file compare dos directory

我想以递归方式比较2个文件夹,并在其中找到丢失的文件。我使用for循环比较2个文件夹但不能搜索子文件夹。有人可以帮忙吗?

这是我尝试过的代码,

@echo off
if "%2" == "" GOTO Usage

cd /D %1
if errorlevel 1 goto usage

for %%x in (*.*) do if NOT exist %2\%%x echo missing %2\%%x
cd /D %2
for %%x in (*.*) do if NOT exist %1\%%x echo missing %1\%%x

goto end

:usage
echo Usage %0 dir1 dir2
echo where dir1 and dir2 are full paths
:end

2 个答案:

答案 0 :(得分:1)

试试这个,为了解释,请参阅代码中的注释:

@ECHO OFF &SETLOCAL
SET "folder1=this"
SET "folder2=that"

REM delete variables
FOR /f "delims==" %%a IN ('set "$"') DO SET "%%a="

REM scanning folder1
FOR /r "%folder1%" %%a IN (*) DO SET "$%%~nxa=%%~a"

REM compare with folder2
FOR /r "%folder2%" %%a IN (*) DO (
    IF NOT DEFINED $%%~nxa ECHO missing IN %folder1%: %%a
)

REM delete variables
FOR /f "delims==" %%a IN ('set "$"') DO SET "%%a="

REM scanning folder2
FOR /r "%folder2%" %%a IN (*) DO SET "$%%~nxa=%%~a"

REM compare with folder1
FOR /r "%folder1%" %%a IN (*) DO (
    IF NOT DEFINED $%%~nxa ECHO missing IN %folder2%: %%a
)

ECHO Done.

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL
XCOPY /l /y /d "%~1\*" "%~2\*"|FIND "\"
XCOPY /l /y /d "%~2\*" "%~1\*"|FIND "\"
GOTO :EOF

严格列出一个目录中但不存在于另一个目录中的文件;列出那些以及那些存在于两者中但具有不同时间戳的那些。

/s添加到XCOPY选项以扫描子目录。