CMD比较文件夹并写入新文件和较小文件的报告

时间:2013-06-24 15:54:23

标签: command-line cmd compare

我想编写一个批处理文件,以两种方式比较两个文件夹:

  1. 仅比较名称并将文件夹只写入第一个文件夹中但第二个文件夹中不存在的文件名 - 我一直在尝试将文件名和文件名写入文本文件使用fc,但都显示我不需要的额外信息。我只需要删除文件的名称。

  2. 比较两个文件夹中具有相同文件名的文件的大小,并提供仅与第一个文件夹相比在第二个文件夹中小于5%的文件的列表。同样,我只需要这些文件的名称(如果可能的话,可能还有百分比差异)。

  3. 我希望将两者的结果写入txt文件。

2 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL
SET "dir1=."
SET "dir2=.\e"
SET "report1=u:\existinfirstnotsecond.txt"
SET "report2=u:\smallerinsecond.txt"
DEL "%report1%" 2>NUL >nul
DEL "%report2%" 2>NUL >nul
FOR /f "delims=" %%i IN ('dir /b /a-d "%dir1%\*"') DO (
 IF EXIST "%dir2%\%%i" (
  FOR %%q IN ("%dir1%\%%i") DO FOR %%s IN ("%dir2%\%%i") DO (
   CALL :sizes %%~zq %%~zs "%%i"
   )
 ) ELSE (
 >>"%report1%" ECHO %%i
 )
)

GOTO :EOF

:sizes
IF %2 GEQ %1 GOTO :EOF
IF %2 equ 0 SET "diff= LOTS"&GOTO report
SET /a diff=%1 - %2
SET /a diff=%diff%*10000/%1
IF %diff% LSS 500 GOTO :EOF
SET diff= %diff%
SET diff=%diff:~-4,2%.%diff:~-2%
:report
>>"%report2%" ECHO %diff%%% %~3
GOTO :eof

这应该产生你想要的结果。您只需要替换dir1,dir2,report1report2

的设置

如果第一个文件名存在于第二个目录中,请将两者的大小和文件名发送到过程:sizes,否则,将第二个目录中找到但丢失的文件的名称写入{{1} }

如果第一个的大小大于或等于第二个的大小,则不报告,如果第二个文件的长度为0,则%diff为无穷大,否则计算差值,乘以10000并将结果除以第一个文件的大小。结果是0..10000; 500表示5%,如果小于5%则忽略它,否则添加前导空格,插入点并报告差异。

如果文件大于200K,则可能出现唯一的问题,其中数学需要更复杂一些(批量限制为32位有符号整数)


编辑20130625-1958Z - For .. %% i / %% q / %% s循环替换原来修复缺少目录名的问题。


编辑20130626-1601Z - 更长文件的替换report1例程

SIZES

答案 1 :(得分:1)

OP的最终版本......

@ECHO OFF 

SETLOCAL

SET "dir1=P:\week3"
SET "dir2=P:\week4"

SET "report1=P:\Test\removed.txt"
SET "report2=P:\Test\existinfirstnotsecond.txt"
SET "report3=P:\Test\smallerinsecond.txt"
SET "report4=P:\Test\fullreport.txt" 

DEL "%report1%" 2>NUL >nul
DEL "%report2%" 2>NUL >nul
DEL "%report3%" 2>NUL >nul
DEL "%report4%" 2>NUL >nul

FOR /f "delims=" %%i IN ('dir /b /a-d "%dir2%\*"') DO (
IF NOT EXIST "%dir1%\%%i" (
    >> "%report1%" ECHO %%i
)
)

FOR /f "delims=" %%i IN ('dir /b /a-d "%dir1%\*"') DO (
IF EXIST "%dir2%\%%i" (
    FOR %%q IN ("%dir1%\%%i") DO (
        FOR %%s IN ("%dir2%\%%i") DO (
            CALL :sizes %%~zq %%~zs %%i
        )
    )
) ELSE (
    >> "%report2%" ECHO %%i
)
)

ECHO Old Folder is: %dir1% > "%report4%"
ECHO New Folder is: %dir2% >> "%report4%"
ECHO. >> "%report4%"
ECHO New files are: >> "%report4%"
TYPE "%report1%" >> "%report4%"
ECHO. >> "%report4%"
ECHO Removed files are: >> "%report4%"
TYPE "%report2%" >> "%report4%"
ECHO. >> "%report4%"
ECHO Files smaller by over 5%% are: >> "%report4%"
TYPE "%report3%" >> "%report4%"

START notepad "%report4%"

GOTO :EOF
::^^^^^:: This extra line inserted...PW

:sizes
SET siz1=%1
SET siz2=%2
SET name=%3

:simplify
IF [%siz1:~5%] NEQ [] (
IF [%siz2:~5%] NEQ [] (
    SET siz1=%siz1:~0,-1%
    SET siz2=%siz2:~0,-1%
    GOTO :simplify
)
)

SET /a diff=10000-(10000*%siz2%/%siz1%)

IF %diff% LSS 500 GOTO :EOF
SET diff=00%diff%
SET diff=%diff:~-4,2%.%diff:~-2% 

:report
>> "%report3%" ECHO %diff%%% %name%

GOTO :EOF

只需要根据指示添加额外的GOTO :EOF行,否则批处理的执行将直接传递到:sizes例程,并可能导致语法错误。