开发者

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:

  1. C:\Work\Build\SASE Lab Tools

  2. C:\Work\Build\SASE Lab Tools.01052011

  3. C:\Work\Build\SASE Lab Tools.02052011

  4. C:\Work\Build\SASE Lab Tools.03052011

  5. C:\Work\Build\SASE Lab Tools.04052011

  6. C:\Work\Build\SASE Lab Tools.05052011

  7. 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜