You are not logged in.

#1 22 Jul 2015 16:34

richardc
New Member
Registered: 22 Jul 2015
Posts: 1

Delete All but Last N Log Files with multiple file base name

Hi,

I have a folder that has log files from multiple processes and each of those processes will generate a file daily/hourly/weekly etc. I want to only have the latest N versions for each process.

examples of the log file names


arrow_20150715130002.log
arrow_20150716130001.log
arrow_20150717130014.log
arrow_20150718130021.log

mark_20150115000001.log
mark_20150215000018.log
mark_20150315000011.log
mark_20150415000005.log

mouse_20150627080001.log
mouse_20150704080012.log
mouse_20150711080031.log
mouse_20150718080022.log

I'm very new to using command line and have tried to find a solution but have not found it. The closest I found is the code below that will leave the latest 2 log files, but it only keeps a total of 2 log files, not 2 for each log file process name. Any help would be greatly appreciated.

thanks!

@echo off

set myDir=C:\deletetest

for /f "tokens=* skip=2" %%F in ('dir %myDir% /o-d /tc /b') do del %myDir%\%%F

Offline

#2 22 Jul 2015 19:21

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: Delete All but Last N Log Files with multiple file base name

@echo off
setlocal EnableDelayedExpansion

set N=2

set myDir=C:\deletetest
cd "%myDir%"

rem Store the last N names per group in "name" array with this structure: name[group,i]=filename
for /F "tokens=1* delims=_" %%a in ('dir /O-D /TC /B') do (
   set /A "%%a=(%%a+1) %% N"
   set "name[%%a,!%%a!]=%%a_%%b"
)

rem Set ReadOnly attribute in such files
for /F "tokens=1* delims==" %%a in ('set name[') do attrib +R "%%b"

rem Delete all files, excepting the previous ones
del /Q *.*

rem Clear the ReadOnly attribute
attrib -R *.*

EDIT: A simpler method! cool

@echo off
setlocal EnableDelayedExpansion

set N=2

set myDir=C:\deletetest
cd "%myDir%"

for /F "tokens=1* delims=_" %%a in ('dir /O-D /TC /B') do (
   set /A "%%a=(%%a+1) %% N"
   for /F %%i in ("!%%a!") do del !name[%%a,%%i]! 2> NUL
   set "name[%%a,!%%a!]=%%a_%%b"
)

Antonio

Last edited by Aacini (22 Jul 2015 22:23)

Offline

#3 23 Jul 2015 12:08

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Delete All but Last N Log Files with multiple file base name

Here's my solution:

@echo off

setlocal enabledelayedexpansion

set keep=2
set match="*.log"
set "dir=X:\your directory"

REM reset any counters
for /f "usebackq tokens=1 delims==" %%v in (`set count_`) do set "%%v=0"
pushd "%dir%" || exit /b 1
REM list the matching files in reverse date order
for /f "usebackq tokens=*" %%F in (`dir /o-d /b /tc "%match%"`) do (
  REM break apart the file name on the first "_"
  for /f "tokens=1 delims=_" %%a in ("%%~nF") do (
    REM count items of prefix "%%a"
    set /a "count_%%a+=1"
    REM display progress for debugging
    echo "%%F" / %%a = !count_%%a!
    REM when "keep" count has been satisfied, start deleting items
    if !count_%%a! GTR %keep% (echo del "%%~fF")
  )
)

popd

Last edited by bluesxman (23 Jul 2015 15:23)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by