Reputation: 13
im looking for help with a batch code that can help me to delete every folder except the last 5 orderdered by name ASC. i have something like this:
- Folder 1
- Folder 2
- Folder 3
- Folder 4
- Folder 5
- Folder 6
- Folder 7
- Folder 8
- Folder 9
- Folder 10
And i want it to end up like this
- Folder 1 [Deleted]
- Folder 2 [Deleted]
- Folder 3 [Deleted]
- Folder 4 [Deleted]
- Folder 5 [Deleted]
- Folder 6
- Folder 7
- Folder 8
- Folder 9
- Folder 10
hope you can help me, and thank you in advance
Upvotes: 1
Views: 1833
Reputation: 41234
This should keep the 5 newest folders.
It will only echo the rd commands to the console and if they are right then you can remove the echo
@echo off
for /f "skip=5 delims=" %%a in ('dir /b /o-d /ad') do echo rd /s /q "%%a"
pause
Upvotes: 1