I have a folder containing a mix of log files - ex:
AAA.0.1.txt
AAA.0.2.txt
BBB.1.2.txt
BBB.1.3.txt
BBB.1.4.txt
CCC.0.5.txt
CCC.0.6.txt
I need a quick little batch file to clean out all but the most recent (by date) 3 files for each of A, B, and C.
I can delete the all but he most recent 3 files in the folder:
for /f "tokens=* skip=3" %%F in ('dir %myDir% /o-d /tc /b') do del %myDir%\%%F
but I'm not sure how to add in the naming convention without hardcoding A, B, and C and doing 3 separate loops (and if D shows up then it gets ignored altogether).
Any ideas?
Okay, I figured it out:
@echo off
setlocal enabledelayedexpansion
cls
set fileDir=C:\My\Log\Path
set prev=A
for /f "tokens=*" %%P in ('dir %fileDir% /o-d /tc /b') do (
set fileName=%%P
for /f "tokens=1,2,3,4 delims=. " %%a in ("!fileName!") do set proj=%%a
if "!proj!" == "!prev!" (
REM skip
) else (
for /f "tokens=* skip=3" %%F in ('dir %fileDir%\!proj!.* /o-d /tc /b') do del %fileDir%\%%F
set prev=!proj!
)
)