예제 #1
0
 private int CleanDirectories(DirectoryInfo Directory, DateTime RetainTime, List <DirectoryInfo> DirectoriesToDelete, int TargetDepth, int CurrentDepth = 0)
 {
     // Go deeper if we need to.
     if (TargetDepth > CurrentDepth)
     {
         int DirsFound = 0;
         foreach (DirectoryInfo SubDirectory in Directory.EnumerateDirectories())
         {
             DirsFound += CleanDirectories(SubDirectory, RetainTime, DirectoriesToDelete, TargetDepth, CurrentDepth + 1);
         }
         return(DirsFound);
     }
     else
     {
         CommandUtils.LogInformation("Scanning {0}...", Directory);
         IEnumerable <DirectoryInfo> DirsToScan = Directory.EnumerateDirectories();
         foreach (DirectoryInfo BuildDirectory in DirsToScan)
         {
             try
             {
                 if (!BuildDirectory.EnumerateFiles("*", SearchOption.AllDirectories).Any(x => x.LastWriteTimeUtc > RetainTime))
                 {
                     DirectoriesToDelete.Add(BuildDirectory);
                 }
             }
             catch (Exception Ex)
             {
                 CommandUtils.LogWarning("Unable to enumerate {0}: {1}", BuildDirectory.FullName, Ex.ToString());
             }
         }
         return(DirsToScan.Count());
     }
 }
        /// <summary>
        /// Entry point for the commandlet
        /// </summary>
        public override void ExecuteBuild()
        {
            string ReportDir = ParseParamValue("ReportDir", null);

            if (ReportDir == null)
            {
                throw new AutomationException("Missing -ReportDir parameter");
            }

            string Days = ParseParamValue("Days", null);

            if (Days == null)
            {
                throw new AutomationException("Missing -Days parameter");
            }

            double DaysValue;

            if (!Double.TryParse(Days, out DaysValue))
            {
                throw new AutomationException("'{0}' is not a valid value for the -Days parameter", Days);
            }

            DateTime RetainTime = DateTime.UtcNow - TimeSpan.FromDays(DaysValue);

            // Enumerate all the build directories
            CommandUtils.LogInformation("Scanning {0}...", ReportDir);
            int NumFolders = 0;
            List <DirectoryInfo> FoldersToDelete = new List <DirectoryInfo>();

            foreach (DirectoryInfo BuildDirectory in new DirectoryInfo(ReportDir).EnumerateDirectories())
            {
                if (!BuildDirectory.EnumerateFiles("*", SearchOption.AllDirectories).Any(x => x.LastWriteTimeUtc > RetainTime))
                {
                    FoldersToDelete.Add(BuildDirectory);
                }
                NumFolders++;
            }
            CommandUtils.LogInformation("Found {0} builds; {1} to delete.", NumFolders, FoldersToDelete.Count);

            // Delete them all
            for (int Idx = 0; Idx < FoldersToDelete.Count; Idx++)
            {
                try
                {
                    CommandUtils.LogInformation("[{0}/{1}] Deleting {2}...", Idx + 1, FoldersToDelete.Count, FoldersToDelete[Idx].FullName);
                    FoldersToDelete[Idx].Delete(true);
                }
                catch (Exception Ex)
                {
                    CommandUtils.LogWarning("Failed to delete folder; will try one file at a time: {0}", Ex);
                    CommandUtils.DeleteDirectory_NoExceptions(true, FoldersToDelete[Idx].FullName);
                }
            }
        }