我想编写一个批处理文件,以两种方式比较两个文件夹:
仅比较名称并仅将第一个文件夹中存在但第二个文件夹中不存在的文件名称写入文件夹 - 我一直在尝试将 comp 和文件名写入文本文件,并且使用 fc,但两者都显示我不需要的额外信息。我只需要已删除文件的名称。
比较两个文件夹中具有相同文件名的文件的大小,并仅列出第二个文件夹中比第一个文件夹小 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/