1.

Solve : Keep latest 2 files and delete the rest?

Answer»

I have the following:

C:\Test\2007 Aug 07 Prod
C:\Test\2007 Aug 06 Prod
C:\Test\2007 Aug 03 Prod
C:\Test\2007 Aug 02 Prod
C:\Test\2007 Aug 01 Prod

how do I write the ms-dos script to keep the latest 2 files and delete the rest of the files?

Thanks.


this works..

for /f "skip=2 tokens=1,1,5* delims=," %%a in ('dir /A:A /OD /B /S') do del /f "%%a"

remember that if you want to test this code first, to REMOVE a % symbol from %%a so that they both become %a , this code above has %%a because its MENT for using in a batch file

@ECHO OFF
CD\YOURFOLDERWITHFILES
for /f "skip=2 tokens=1,1,5* delims=," %%a in ('dir /A:A /OD /B /S') do del /f "%%a"
EXIT

this will keep the two NEWEST files and delete the rest of them ,



Discussion

No Comment Found