2013-06-24 47 views
2

我想編寫兩個文件夾在兩個方面比較批處理文件報告:CMD比較文件夾,並寫入新文件更小的文件

  1. 比較公正的名稱和寫入到文件夾只有名字存在於第一個文件夾但不存在於第二個文件夾中的文件 - 我一直在嘗試使用comp和將文件名寫入文本文件並使用fc,但都顯示我不需要的額外信息。我只需要刪除文件的名稱。

  2. 比較兩個文件夾中具有相同文件名的文件的大小,並給出第二個文件夾中與第一個文件夾相比較小5%以上的文件的列表。再次,我只需要這些文件的名稱(如果可能的話可能還有百分比差異)。

我想將兩者的結果寫入一個txt文件。

回答

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例程,並可能導致語法錯誤。

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,否則,寫找到的文件的名稱,但在失蹤第二個目錄report1

如果第一個的大小大於或等於第二個的大小,則不報告它,如果第二個文件的長度爲0,則%diff是無限的,否則計算的差異,乘以10000並將結果除以第一個文件的大小。結果是0..10000; 500表示5%,如果小於5%則忽略它,否則添加前導空格,插入點並報告差異。

如果文件是可能出現的唯一問題> 200K,其中數學需要一點更復雜(批次被限制爲32位有符號整數)


編輯20130625-1958Z - 對於.. %%我/ %% q/%% s循環替換原來修復缺少目錄名稱的問題。


編輯20130626-1601Z - 更換SIZES常規更長的文件

:sizes 
:: establish the file sizes 
SET siz1=%1&SET siz2=%2 
:szloop 
:: Size of file 1 insanely less than size of file 2 ? 
IF NOT DEFINED siz1 GOTO :EOF 
:: Size of file 2 insanely less than size of file 1 ? 
IF NOT DEFINED siz2 SET "diff= LOTS"&GOTO report 
:: keep peeling the last digit from the sizes until 
:: neither is more than 5 digits long 
IF NOT %siz1:~5%%siz2:~5%x==x SET siz1=%siz1:~0,-1%&SET siz2=%siz2:~0,-1%&GOTO szloop 
:: Now use SIZ1 and SIZ2 
IF %siz2% GEQ %siz1% GOTO :EOF 
IF %siz2% equ 0 SET "diff= LOTS"&GOTO report 
SET /a diff=siz1 - siz2 
SET /a diff=%diff%*10000/siz1 
IF %diff% LSS 500 GOTO :EOF 
SET diff= %diff% 
SET diff=%diff:~-4,2%.%diff:~-2% 
:report 
>>"%report2%" ECHO %diff%%% %~3 
GOTO :eof 
+0

非常感謝您的回覆!我剛剛對我的文件進行了測試,它對第一次報告非常有用,但是在secodn中,如果它給了我比較的文件名稱,那將是非常好的。我知道我需要在這裏添加文件名:>>「%report2%」ECHO%diff %%%%〜3,但不知道要寫什麼...... – user2516836

+0

不確定需要更改哪些內容。當前正在輸出的行應該顯示諸如「10.67%文件名」這樣的行 - 它適用於我。這兩個目錄中的文件名是相同的;你知道這些目錄名是什麼 - 只有當第二個文件小於第一個文件大小的95%時纔會顯示報告。如果你想要完整的文件名,那麼使用'%dir1%\%〜3'或'%dir2%\%〜3'或者兩者來代替'%〜3'。如果你願意,在':sizes'這個標籤後面加上一行'ECHO%*'來顯示傳遞給例程的參數,這些參數可以用'%1,%2和%3'表示'〜'描述 – Magoo

+0

我現在可以看到問題所在。參數不適合我。沒有顯示參數1,對於2和3,它分別是dir2中的文件大小和文件名。這也是爲什麼百分比值不能正確計算(因爲只有一個大小值被解析)。你知道這個問題可能是什麼嗎? – user2516836

相關問題