Ejemplo n.º 1
0
        private void PrintStats()
        {
            char topleft     = '┌';
            char hline       = '─';
            char topright    = '┐';
            char vline       = '│';
            char bottomleft  = '└';
            char bottomright = '┘';

            var builder = new StringBuilder();

            builder.Append(topleft);
            for (int i = 0; i < 104; i++)
            {
                builder.Append(hline);
            }
            builder.Append(topright);
            _log.Info(builder.ToString());

            _log.Info(String.Format("{0}{1,-14}{2,-90}{0}", vline, "Summary as of ", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")));
            _log.Info(String.Format("{0}{1,-22}{2,-60}{3,11}{4,11}{0}", vline, "Time", "Folder", "Total Files", "Total Mb"));
            foreach (DateTime item in _stats.Keys)
            {
                CleanupStats currentStat = (CleanupStats)_stats[item];
                _log.Info(String.Format("{0}{1,-22}{2,-60}{3,11}{4,11:n0}{0}", vline, item.ToString("dd/MM/yyyy HH:mm:ss"), currentStat.folder, currentStat.totalFiles, currentStat.totalBytes / (1024 * 1024)));
            }

            builder.Clear();
            builder.Append(bottomleft);
            for (int i = 0; i < 104; i++)
            {
                builder.Append(hline);
            }
            builder.Append(bottomright);
            _log.Info(builder.ToString());
        }
Ejemplo n.º 2
0
        private void PurgeFiles(string dirPath)
        {
            try
            {
                DateTime today      = DateTime.Now.Date;
                TimeSpan keepBuffer = new TimeSpan(_daysToKeep, 0, 0, 0);

                if (!_dailyTotals.Contains(today))
                {
                    _dailyTotals[today] = (long)0;
                }

                string[] folders = Directory.GetDirectories(dirPath, "*", SearchOption.AllDirectories);

                foreach (string folder in folders)
                {
                    string[] files = Directory.GetFiles(folder, "*", SearchOption.TopDirectoryOnly);

                    if (files.Length == 0)
                    {
                        // don't process empty folders
                        continue;
                    }

                    _log.Info(String.Format("Purging folder {0} -> {1} files", folder, files.Length));

                    CleanupStats stats = new CleanupStats()
                    {
                        totalBytes = 0, totalFiles = 0
                    };

                    foreach (string file in files)
                    {
                        try
                        {
                            FileInfo fi = new FileInfo(file);
                            stats.totalBytes += fi.Length;
                            stats.totalFiles++;
                            stats.folder = folder;

                            try
                            {
                                if (fi.LastWriteTime > today - keepBuffer)
                                {
                                    // recently created archive, so don't delete
                                    _log.Debug("Not deleting recent file " + file);
                                    continue;
                                }

                                if (_simulate == true)
                                {
                                    _log.Debug("Would have deleted " + file);
                                }
                                else
                                {
                                    File.Delete(file);
                                    _dailyTotals[today] = (object)((long)_dailyTotals[today] + stats.totalBytes);
                                }
                            }
                            catch (Exception ex)
                            {
                                _log.Error("File.Delete try PurgeFiles: " + ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            _log.Error("Inner try PurgeFiles: " + ex.Message);
                        }
                    }

                    _stats.Add(DateTime.Now, stats);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Outer try PurgeFiles: " + ex.Message);
            }
        }