Windows Batch Script to only keep the N latest FOLDERS in a directory

前端 未结 1 968
天命终不由人
天命终不由人 2021-01-03 19:12

I have a directory into which I backup MySQL dbs in a folder with the current date.

Now after 30 or 60 days I like to only keep the n latest folders in that backup d

相关标签:
1条回答
  • 2021-01-03 19:21

    These two different lines work. Keeping all files there might be in the backupDir while deleting only the selected N latest nameDir folders.

    This is adjusted from @JosefZ's comment Windows Batch Script to only keep the N latest FOLDERS in a directory

    for /f "skip=2 delims=" %%a in ('dir "%backupDir%\" /B /O:-N /A:D') do echo rd rd /s /q "%backupDir%\%%a"
    

    This also works taken from @Magoo's answer from here https://stackoverflow.com/a/17521693/1010918

    for /f "skip=2 delims=" %%a in (' dir "%backupDir%\" /b /ad-h /o-d') do rd /s /q "%backupDir%\%%a"
    

    Find the complete working Batch Script here Windows Batch Script to backup local MySQL databases & only keep N latest FOLDERS with backup files if you like.

    Thank you all for your help!

    0 讨论(0)
提交回复
热议问题