On Sat, 02 Jun 2012 17:38:19 +0200, Helge Haensel wrote:
>
> W7/64/HP
> I have a folder containing more or less files with names YYYYMMDDtext.zip
> All filenames differ by the date information only.
> I want to keep the 4 youngest ones and delete the older if any - without
> a yes/no prompt as a part of my backup strategy.
> Consider the case that intermediate dates may be missing by any number.
I don't know an easy way to do this in native Windows command line.
You could probably do it in PowerShell, but I know nothing of
PowerShell beyond the name.
The most straightforward way I know is to do something like
(untested):
dir /b /o:-dt >zonk.txt
gawk -f zonk.awk <zonk.txt >zonk.bat
zonk
where zonk.awk contains the following (untested):
BEGIN {print "@echo off"}
// { if (NR > 4) print "del " $0 }
The dir command gets the files into a file called zonk.txt, from
youngest to oldest. The gawk command ignores the first four and
prefixes a del command to the others. Then the batch file calls the
zonk.bat file just written.
gawk is free and open source; Win32 builds of it already exist in the
GNU project.
Aside from a possible typo in the above, I'm confident it will work
because it's the sort of thing I do fairly often, though I don't have
your specific requirement.
--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
Shikata ga nai...