Hello !
I wrote a batch script to to be run into a folder where I have many log files generated automatically but they have these name format : *-*-*-*.*
I would like to delete all files having the SAME first delimiter ex a1-*-*-.* BUT keep the latest version (the most recent file). Example, I could find in my folder these files :
a1-dd-ff-rr.log
a1-ff-rr-ww.log
a1-87-43-ws.log
a1-re-ee-ww.log
a2-ww-11-lk.log
a2-uz-21-ws.log
.
.
.
q-wq-we-tf.*
q-ew-ws-we.*
..
.
po-we-we-ed.log
po-87-pl-lo.log
po-ws-ed-rf.log
po-ws-aa-qq.log
So the idea was to filter first the beginning of each file and put them in a list, sort that list by date, delete all but keep the most recent one. In the batch script below, it does the work but only for the first category (a1) it doesn’t loop the second (a2), the third (q),,,,,, here is the script :
==================
@echo off
setlocal enabledelayedexpansion
set Repertoire=c:\test
set NombreDeFichiersALaisser=2
if not exist "%Repertoire%" (
echo Le repertoire %Repertoire% n''existe pas
echo Mettez à jour le script et réssayer
echo.
echo Appuyer sur une touche pour continuer ....
pause>nul
)
cd /d "%Repertoire%"
set compteur=0
FOR /f "tokens=1,2,3,4 delims=-" %%I in ('dir /b *-*-*-*.*') do call :Imprimer %%I
call :SupprimerDoublons RecupEnTete.txt
:Imprimer
echo %1>>RecupEnTete.txt
rem del %1 /f >nul
exit /b
:SupprimerDoublons
@echo off > SortieSansDoublons.txt
if %1'==' echo which file? && goto :eof
if not exist %1 echo %1 not found && goto :eof
for /f "tokens=* delims= " %%a in (%1) do (
find "%%a" < SortieSansDoublons.txt > nul
if errorlevel 1 echo %%a>>SortieSansDoublons.txt
)
@echo off
for /f %%I in (SortieSansDoublons.txt) do (
for %%A in (%%I-*-*-*.*) do (call
rocess %%A)
)
set errorlevel=0
goto :eof
rocess
set /a compteur+=1
if %compteur% leq %NombreDeFichiersALaisser% exit /b
echo %1>>FichiersSupprimesLe%date:~0,2%%date:~3,2%%date:~6,4%.txt
rem del %1 /f >nul
exit /b
=============================
Thank you very much in advance.