I would like to keep the X latest files from a folder and delete the rest. Is this possible with FORFILES
? If it's not I can fallback to another solution I seen here. Thanks for help.
I did this but it takes by dates: EDIT
forfiles /p [path] /s /d -5 /c "cmd /c echo @file"
(echo file for testing purpose)
@ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET /a retain=10
FOR /f "skip=%retain%delims=" %%a IN (
'dir /b /a-d /o-d "%targetdir%\*" '
) DO ECHO (DEL "%targetdir%\%%a"
GOTO :EOF
You would need to change the setting of targetdir
to suit your circumstances. Equally, this procedure targets all files - change the filemask to suit.
The required DEL commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL
to DEL
to actually delete the files.
method is to simply execute a dir
in basic form without directories, sorted in reverse-date order.
Skip the first 10 entries, and delete the rest.