简体   繁体   中英

deleting the oldest folder by identifying from the folder name

an example of my folder names are as follow:

abc.300520111500
abc.310520111500
abc.310520111515

as u can see, these folders are backed up by date time stamp.

original folder: abc

backup folder: abc.ddMMyyyyhhmm

my current code looks like this:

var files = Directory.GetDirectories(path, "abc.*");
foreach(var file in files)
Console.WriteLine(file);
foreach(var file in files.OrderByDescending(x=>x).Skip(int.Parse(args[0])))
Console.WriteLine(file);
foreach(var file in files.OrderByDescending(x=>x).Skip(int.Parse(args[0])))
Directory.Delete(file, true);

where args[0] will identify the number of folders to jump to. This code will always delete the smallest folder number.

However, i met a problem recently and that is when we have a change in month. for example:

abc.020620111500 where 020620111500 is the smallest folder number now.

as a result, abc.020620111500 will always be deleted instead of abc.300520111500

anybody has any idea how to go about solving this issue?

If you can't change the directory names to YYYYMMDD which will always yield the result you want, you can try manipulating / parsing the filename strings (converting to dates for comparison, perhaps) and then rebuilding the directory name to be deleted.

you can split the datetime part from the list of folders and have an unordered list.And then order them by datetime. something like this..

var orderedList =
    (from p in unorderedList
     let value = DateTime.Parse(p.Details.Find(s => s.Name == sortColumn).Value)
     orderby value
     select p)
     .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM