Not sure this is what you need, but can be used as a skeleton to start
@echo off
setlocal enableextensions disabledelayedexpansion
pushd "c:\where\the\filesAre" && (
for /f "skip=1 delims=" %%a in ('dir /b /a-d /tw /o-d *') do echo del "%%a"
popd
)
First, the active directory is changed to the adecuated folder (change to your needs). Then a for
command is used to execute a dir
command and process its output. For each line in the dir
output, the code after the do
clause is executed, with the content of the line stored in the for
replaceable parameter %%a
The dir
command will list the files in bare format (/b
only file names), excluding directories (/a-d
), in descending last write date order (/tw /o-d
). So, the newest file is the first in the list (the output of the dir command) that the for
command will process.
As we are asking for
to skip one line (skip=1
), this first file will be ignored. For the rest of the lines/files a del command is executed.
del
operations are only echoed to console. If the output is correct, remove the echo
command to delete the files.
edited to adapt to comments
@echo off
setlocal enableextensions disabledelayedexpansion
rem Configure extensions to process
set "_ext.00=al*.txt"
set "_ext.01=cm*.txt"
rem Change to the desired folder
pushd "c:\where\the\filesAre" || goto :eof
rem For each pattern in the list
rem For each file matching the pattern (except the newest)
rem Delete the file
for /f "tokens=1,* delims==" %%x in ('
set _ext.
') do for /f "skip=1 delims=" %%a in ('
dir /b /a-d /tw /o-d "%%~y" 2^>nul
') do echo del "%%a"
popd
Define an "array" with the list of file patterns to process. For each of the patterns (retrieved with a set
command inside the outter for /f
) iterate over the files matching that pattern (the inner for
), skipping the first/newest and removing the rest.