サードパーティ製ソフトウェアのインストールを必要としないソリューションが必要な場合は、以下のスクリプトを使用してください。組み込みのコマンドライン ツールのみを使用します。
スクリプトは、最初にいくつかの一般的なエラー状態をチェックします。次に、クリーンアップ ディレクトリ内のすべてのファイルを再帰的に繰り返します。バックアップ ディレクトリに同じ名前のファイルが見つかった場合は、バイナリ比較を行ってファイルが冗長かどうかを判断します。
@echo off
rem delete files from a directory that have a redundant copy in a backup directory
setlocal enabledelayedexpansion
rem check arguments
if "%~2"=="" (
echo.Usage: %~n0 cleanup_dir backup_dir
echo.Delete files from cleanup_dir that have a redundant copy in backup_dir
exit /b 1
)
set CLEANUP_DIR=%~f1
if not exist "%CLEANUP_DIR%" (
echo."%CLEANUP_DIR%" does not exist.
exit /b 1
)
set BACKUP_DIR=%~f2
if not exist "%BACKUP_DIR%" (
echo."%BACKUP_DIR%" does not exist.
exit /b 1
)
rem ensure that dirs are different
if "%CLEANUP_DIR%" == "%BACKUP_DIR%" (
echo.backup directory must not be the same as cleanup directory.
exit /b 1
)
rem ensure that backup_dir is not a sub dir of cleanup_dir
if not "!BACKUP_DIR:%CLEANUP_DIR%=!" == "%BACKUP_DIR%" (
echo.backup directory must not be a sub directory of cleanup directory.
exit /b 1
)
rem iterate recursively thru files in cleanup_dir
for /R "%CLEANUP_DIR%" %%F in (*) do (
set FILE_PATH=%%F
set BACKUP_FILE_PATH=!FILE_PATH:%CLEANUP_DIR%=%BACKUP_DIR%!
if exist "!BACKUP_FILE_PATH!" (
rem binary compare file to file in backup dir
fc /B "!FILE_PATH!" "!BACKUP_FILE_PATH!" >NUL 2>&1
if not errorlevel 1 (
rem if files are identical delete file from cleanup_dir
echo.delete redundant "!FILE_PATH!".
del "!FILE_PATH!"
) else (
echo.keep modified "!FILE_PATH!".
)
) else (
echo.keep added "!FILE_PATH!".
)
)