Bat file to delete files only when younger files are present

后端 未结 2 958
醉梦人生
醉梦人生 2020-12-12 06:27

Our backup system creates .bak files everyday which we can use to restore files if we ever run into issues. If left alone these would fill up our storage so I found a batch

2条回答
  •  萌比男神i
    2020-12-12 06:44

    Here's an untested example script which should work as long as you don't have file names where [DESC] contains _, = or other problematic characters.

    @Echo Off
    SetLocal DisableDelayedExpansion
    For /F "Delims==" %%A In ('Set _[ 2^>Nul') Do Set "%%A="
    If /I Not "%CD%"=="C:\xxx\yyy" (Set "_[:]=T"
        PushD "C:\xxx\yyy" 2>Nul||Exit /B)
    For /F "Tokens=1* Delims=_" %%A In ('Dir /B /O-N *_backup_*_*_*_*_*.bak'
    ) Do If Defined _[%%A] (Del /A /F "%%A_%%B") Else Set "_[%%A]=T"
    If Defined _[:] PopD
    EndLocal
    Exit /B
    

提交回复
热议问题