もちろん。すべてがバッチで可能です。:D
このバッチは実際にはファイルを削除しません。比較の結果をエコーするだけです。同じファイルが2つ見つかった場合は、どちらかのファイルを削除できます。
としてコードを保存しCleanDuplicates.bat
、でプログラムを開始しますCleanDuplicates {Folder}
現状のまま提供され、保証はありません!サーバー全体が台無しになっているので、ドアをノックしてほしくない。;-)
コードは実際にそれ自体を再帰的に呼び出します。これは別の方法で行うこともできますが、うまくいきます。また、クリーンアップが容易になるため、新しいcmdで再起動します。スクリプトをWindowsVistaBusinessでテストしましたが、Server2003でも動作するはずです。ねえ、それもヘルプ機能を持っています。;-)それぞれがすべてのファイルを返す2つのループが含まれているため、実際の削除を実装すると、以前の反復で削除されたため、一部のファイルが存在しないことが報告される場合があります。
@echo off
rem Check input. //, /// and //// are special parameters. No parameter -> help.
if %1check==//check goto innerloop
if %1check==///check goto compare
if %1check==////check goto shell
if %1check==/hcheck goto help
if %1check==check goto help
rem Start ourselves within a new cmd shell. This will automatically return to
rem the original directory, and clear our helper environment vars.
cmd /c %0 //// %1
echo Exiting
goto end
:shell
rem Save the current folder, jump to target folder and set some helper vars
set FCOrgFolder=%CD%
cd %2
set FCStartPath=%0
if not exist %FCStartPath% set FCStartPath=%FCOrgFolder%\%0
rem Outer loop. Get each file and call ourselves with the first special parameter.
for %%a in (*.*) do call %FCStartPath% // "%2" "%%a"
goto end
:innerloop
rem Get each file again and call ourselves again with the second special parameter.
for %%b in (*.*) do call %FCStartPath% /// %2 %3 "%%b"
goto end
:compare
rem Actual compare and some verbose.
if %3==%4 goto end
echo Comparing
echo * %3
echo * %4
fc %3 %4 >nul
rem Get results
if errorlevel 2 goto notexists
if errorlevel 1 goto different
echo Files are identical
goto end
:different
echo Files differ
goto end
:notexists
echo File does not exist
goto end
:help
echo Compares files within a directory.
echo Usage: %0 {directory}
goto end
:end