Search code examples
windowsbatch-filefilesystemsxcopyrobocopy

delete older files who exist in another directory?


I have two folders who have almost the same files and folders.

I want to consolidate them and delete all files in folderA that are the same in folderB (Same Date or Older)

Someone already asked Here about just deleting files without checking for date.

I want something similar but it should only delete if its the same age or older.

Thank You


Solution

  • try this:

    @echo off&setlocal enabledelayedexpansion
    pushd "%~dp0"
    
    
    for /f %%i in ('dir /b a') do (call:CmpFTime LSS "a\%%~ni" "b\%%~ni"&&del "a\%%~ni")
    
    
    :CmpFTime
    SETLOCAL
    set op=%~1
    set fileL=%~2
    set fileR=%~3
    set attrL=%~4
    set attrR=%~5
    if "%op%"=="" set op===
    if "%attrL%"=="" set attrL=/tw
    if "%attrR%"=="" set attrR=%attrL%
    for /f "tokens=1-6 delims=/: " %%a in ('"dir %attrL% /-c "%fileL%"|findstr "^^[0-1]""') do (
        set TL=%%c%%a%%b%%f%%d%%e
    )
    for /f "tokens=1-6 delims=/: " %%a in ('"dir %attrR% /-c "%fileR%"|findstr "^^[0-1]""') do (
        set TR=%%c%%a%%b%%f%%d%%e
    )
    if "%TL%" %op% "%TR%" (rem.) ELSE set=2>NUL
    EXIT /b
    

    if this contains any errors, feel free to comment, and i will do my best to fix them. i substituted a for foldera and b for folderb.