console application to check for 7 folders and delete the oldest one if i have more than 7
I need to come out with a console application to check for 7 SASE Lab Tools
folders inside C:\Work\Build
.
These different SASE lab Tools
folders are date stamped
An example is shown below:
C:\Work\Build\SASE Lab Tools
C:\Work\Build\SASE Lab Tools.01052011
C:\Work\Build\SASE Lab Tools.02052011
C:\Work\Build\SASE Lab Tools.03052011
C:\Work\Build\SASE Lab Tools.04052011
C:\Work\Build\SASE Lab Tools.05052011
C:\Work\Build\SASE Lab Tools.06052011
开发者_如何学Python
where the date stamp format is ddMMyyyy
so if for example i have C:\Work\Build\SASE Lab Tools.07052011
now(8th and latest one) how do i make sure that i can delete the C:\Work\Build\SASE Lab Tools.01052011
folder? I need to force delete this folder too.. as there are readonly files inside it.
Thanks!
Could you sort them then Skip
7 and then delete the remainder?
static string path = @"C:\Work\Build";
public static void Main(string[] args)
{
var files = Directory.GetFiles(path, "SASE Lab Tools.*");
// Remove after testing
foreach(var file in files)
Console.WriteLine(file);
Console.WriteLine("");
foreach(var file in files.OrderByDescending(x=>x).Skip(7))
Console.WriteLine(file);
// END Remove after testing
foreach(var file in files.OrderByDescending(x=>x).Skip(7))
File.Delete(file);
}
精彩评论