0

我想递归比较 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
4

2 回答 2

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.
于 2013-07-24T08:39:02.193 回答
0
@ECHO OFF
SETLOCAL
XCOPY /l /y /d "%~1\*" "%~2\*"|FIND "\"
XCOPY /l /y /d "%~2\*" "%~1\*"|FIND "\"
GOTO :EOF

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

添加/sXCOPY选项以扫描子目录。

于 2013-07-24T14:43:34.617 回答