Exemplo n.º 1
0
        private object Cleanup(FolderSetting Folder, List <FileInfo> list, operation operation)
        {
            object ob = new object();

            try
            {
                switch (operation)
                {
                case operation.Archive: Archive(Folder, list);
                    break;

                case operation.Compress: Compress(Folder, list);
                    break;

                case operation.Delete: Delete(Folder, list);
                    break;
                }
            }
            catch (Exception ex)
            {
                Utilities.ReportException(ex, _log);
            }

            return(ob);
        }
Exemplo n.º 2
0
 private void Delete(FolderSetting Folder, List <FileInfo> list)
 {
     foreach (var File in list)
     {
         _log.Write(Severity.Debug, "Deleting {0}", File.FullName);
         File.Delete();
     }
 }
Exemplo n.º 3
0
        private bool isExcluded(FolderSetting Folder, FileInfo File)
        {
            var excluded = false;

            foreach (var pattern in Folder.Exclusions)
            {
                if (excluded = Regex.IsMatch(File.FullName, pattern, RegexOptions.IgnoreCase))
                {
                    _log.Write("{0} will be excluded.", File.FullName);
                    break;
                }
            }

            return(excluded);
        }
Exemplo n.º 4
0
        private void Archive(FolderSetting Folder, List <FileInfo> list)
        {
            var prevLastWritetime = DateTime.Today;
            var archivePath       = "";

            foreach (var File in list)
            {
                if (File.LastWriteTime.ToShortDateString() != prevLastWritetime.ToShortDateString())
                {
                    archivePath = GetArchivePath(Folder, File.FullName);
                    _log.Write(Severity.Debug, "Created directory: {0}", archivePath);
                }

                _log.Write(Severity.Debug, "Moving {0} to {1}", File.FullName, archivePath);
                File.MoveTo(Path.Combine(archivePath, File.Name));
            }
        }
Exemplo n.º 5
0
        private void deleteEmptyFolders(FolderSetting folder)
        {
            var backupFolderNamePattern = @"\d{4}-?\d{2}-?\d{2}|\d{4}|\d{2}$";
            var otherFolders            = Directory.GetDirectories(folder.Folder);

            foreach (var otherFolder in otherFolders)
            {
                if (Regex.IsMatch(otherFolder, backupFolderNamePattern))
                {
                    var di = new DirectoryInfo(otherFolder);

                    if ((di.GetFiles().Length == 0) && (di.GetDirectories().Length == 0))
                    {
                        _log.Write(Severity.Debug, "Removing empty directory: {0}.", otherFolder);
                        di.Delete();
                    }
                }
            }
        }
Exemplo n.º 6
0
        private string GetArchivePath(FolderSetting Folder, string filename)
        {
            string archivePath = GetAbsoluteArchivePath(Folder, filename);
            var    fi          = new FileInfo(filename);
            var    yyyy        = fi.LastWriteTime.Year.ToString();
            var    MM          = fi.LastWriteTime.ToString("MM");
            var    yyyyMMdd    = fi.LastWriteTime.ToString("yyyy-MM-dd");

            archivePath = Path.Combine(archivePath, yyyy, MM, yyyyMMdd);

            lock (_lockObj)
            {
                if (!Directory.Exists(archivePath))
                {
                    Directory.CreateDirectory(archivePath);
                    Directory.SetCreationTime(archivePath, fi.LastWriteTime);
                    Directory.SetLastWriteTime(archivePath, fi.LastWriteTime);
                }
            }

            return(archivePath);
        }
Exemplo n.º 7
0
        private void DispatchCleanup(FolderSetting folder, operation operation, int threads)
        {
            var maxDate       = DateTime.Today;
            var count         = 0;
            var operationText = "";


            switch (operation)
            {
            case operation.Archive:
                maxDate       = DateTime.Today.AddDays(-1 * folder.ArchiveAfterDays);
                operationText = "Archiving";
                break;

            case operation.Compress:
                maxDate       = DateTime.Today.AddDays(-1 * folder.CompressAfterDays);
                operationText = "Compressing";
                break;

            case operation.Delete:
                maxDate       = DateTime.Today.AddDays(-1 * folder.DeleteAfterDays);
                operationText = "Deleting";
                break;
            }

            var threadLists = this._fiList.CreateLists(folder, out count, threads, maxDate);

            _log.Write("{0}: {1} {2:N0} files, last updated on or before {3:MM/dd/yyyy}."
                       , folder.Folder, operationText, count, maxDate);

            Parallel.ForEach(threadLists, (list) => Cleanup(folder, list, operation));
            threadLists.Clear();

            if (folder.DeleteEmptyFolders)
            {
                deleteEmptyDirectory(folder.Folder, folder.IncludeSubfolders);
            }
        }
Exemplo n.º 8
0
        public List <List <FileInfo> > CreateLists(FolderSetting folder, out int fileCount, int threads, DateTime maxDate)
        {
            var di = new DirectoryInfo(folder.Folder);
            IEnumerable <FileInfo> fileList;

            if (folder.IncludeSubfolders)
            {
                fileList = from f in di.GetFiles("*.*", SearchOption.AllDirectories)
                           where f.LastWriteTime.Date <= maxDate
                           orderby f.LastWriteTime.Date
                           select f;
            }
            else
            {
                fileList = from f in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)
                           where f.LastWriteTime.Date <= maxDate
                           orderby f.LastWriteTime.Date
                           select f;
            }

            var files = fileList.ToList();

            _log.Write(Severity.Debug, "Found {0:N0} files created before {1:MM/dd/yyyy}", files.Count, maxDate);

            foreach (var file in fileList.ToList())
            {
                if (isExcluded(folder, file))
                {
                    files.Remove(file);
                }
            }

            fileCount = files.Count();
            var threadLists = split(files, threads);

            files.Clear();
            return(threadLists);
        }
Exemplo n.º 9
0
        private static string GetAbsoluteArchivePath(FolderSetting Folder, string filename)
        {
            var archivePath = "";

            if (Folder.ArchivePath.StartsWith(@".\"))
            {
                archivePath = Path.GetDirectoryName(filename);
                archivePath = Path.Combine(archivePath, Folder.ArchivePath.Substring(2));
            }
            else
            if (Folder.ArchivePath.StartsWith(@"..\"))
            {
                archivePath = Path.GetDirectoryName(filename);
                archivePath = Directory.GetParent(archivePath).FullName;
                archivePath = Path.Combine(archivePath, Folder.ArchivePath.Substring(3));
            }
            else
            {
                archivePath = Folder.ArchivePath;
            }

            return(archivePath);
        }
Exemplo n.º 10
0
 private void Compress(FolderSetting Folder, List <FileInfo> list)
 {
     throw new NotImplementedException();
 }