command-line - CMD 比较文件夹并编写新文件和较小文件的报告

标签 command-line cmd compare

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

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

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

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

最佳答案

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 例程并可能呈现语法错误。

关于command-line - CMD 比较文件夹并编写新文件和较小文件的报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17279761/

相关文章:

compare - 使用显示比较结果的比较插件从命令行运行 NotePad++

python - 如何获取属于所有 numpy 数组的项目?

linux - sed 命令查找特定单词

linux - 在unix下执行有管道的命令会使用多少个进程?

c# - 用于 .NET 的强大且友好的命令行工具?

mysql - 将 mysqldbcompare 实用程序的输出重定向到文本文件

python - 如何从 python 脚本内部运行 python 命令?

linux - 如何从 bash 脚本启动程序,然后关闭终端但让程序保持事件状态

windows - 用于在子文件夹中查找文件夹并获取路径的批处理脚本

JavaScript 无法正确比较两个字符串