>@ECHO OFF >DIR /B /OD LPT$VPN.* > C:\TMP\MYLIST.TXT >SET LastF= >FOR /F %%i IN (C:\TMP\MYLIST.TXT) DO SET LastF=%%i >IF [%LastF%]==[] GOTO Quit >FOR /F %%i IN (C:\TMP\MYLIST.TXT) DO IF NOT [%%i]==[%LastF%] DEL %%i >SET LastF= >:Quit >DEL C:\TMP\MYLIST.TXT >The FOR /F was key, as you suggested. I reduced it to the following (8 commands in total, only 2 are really necessary):
@ECHO OFF REM Set the wildcard string of the files to be deleted: set FileWild="LPT$VPN.???" REM Set the value below for the "n" MOST RECENT files to be NOT deleted (i.e. skipped): REM set /? REM http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/set.mspx?mfr=true set /a NumSkip="2" REM This command writes all files matching the wildcard string to FileList.txt REM in REVERSE DATE ORDER, i.e. most recent at the top: REM dir /? dir %FileWild% /o-d /b >FileList.txt REM This command parses the FileList.txt file line-by-line REM for /? REM http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true REM /F instructs the for command to parse REM "skip=%NumSkip%" to skip the first NumSkip lines REM (so those files won't be processed i.e. deleted): for /F "skip=%NumSkip%" %%I in (filelist.txt) do erase %%I REM Cleanup: erase FileList.txt set NumSkip= set FileWild=