Skip to content

Instantly share code, notes, and snippets.

@avin
Last active August 29, 2015 14:28
Show Gist options
  • Save avin/1cd98892a3a0296f65cf to your computer and use it in GitHub Desktop.
Save avin/1cd98892a3a0296f65cf to your computer and use it in GitHub Desktop.
Remove oldest files in folder by total files count
@echo off
setlocal
set Folder=c:\test\oldfiles
set MaxFiles=3
set FileExt=.png
:start
dir "%Folder%\*%FileExt%" /a-d | find /c "%FileExt%" > NUMfiles.###
set /p count=<NUMfiles.###
echo %count%
set OldestFile=
for /f "delims=" %%a in ('dir /b /o:d "%Folder%" 2^>NUL') do (
set OldestFile=%%a
goto Break
)
:Break
if %count% LEQ %MaxFiles% (
echo No old files!
) else (
del "%Folder%\%OldestFile%"
goto start
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment