Ejemplo n.º 1
0
        public static DeleteFilesOptions Parse(IEnumerable <string> arguments)
        {
            var parms = new DeleteFilesOptions();

            var options = new OptionSet()
                          .Add("r", p => parms.Recursive = true)
                          .Add("f", p => parms.RemoveEmptyDirectories = true)
                          .Add("y", p => parms.UseRecycleBin          = true)
                          .Add("d=", d => parms.Days    = Convert.ToInt32(d))
                          .Add("s=", s => parms.Seconds = Convert.ToInt32(s))
                          .Add("help|?", p => ShowHelp());

            var fullpath = options.Parse(arguments);

            if (!fullpath.Any())
            {
                Console.WriteLine("pathspec not specified!");
                ShowHelp();
            }

            parms.Path     = System.IO.Path.GetDirectoryName(fullpath[0]);
            parms.FileSpec = System.IO.Path.GetFileName(fullpath[0]);

            return(parms);
        }
Ejemplo n.º 2
0
        public bool ProcessFiles(DeleteFilesOptions options)
        {
            if (!Directory.Exists(options.Path))
            {
                ShowMessage("Path does not exist: " + options.Path);
            }

            return(ProcessFolder(options.Path, options));
        }
Ejemplo n.º 3
0
        protected bool ProcessFolder(string activeFolder, DeleteFilesOptions options)
        {
            bool success = true;

            foreach (var file in Directory.EnumerateFiles(activeFolder, options.FileSpec))
            {
                try {
                    if (IsFileToBeDeleted(file, options))
                    {
                        if (options.UseRecycleBin)
                        {
                            FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                        }
                        else
                        {
                            File.Delete(file);
                        }

                        ShowMessage("Deleting " + file);
                    }
                }
                catch (Exception) {
                    ShowMessage("Failed to delete file: " + file);
                    success = false;
                }
            }

            if (options.Recursive)
            {
                foreach (var dir in Directory.EnumerateDirectories(activeFolder))
                {
                    success = ProcessFolder(dir, options);
                    if (success && options.RemoveEmptyDirectories)
                    {
                        if (!Directory.EnumerateFiles(dir).Any() && !Directory.EnumerateDirectories(dir).Any())
                        {
                            try {
                                if (options.UseRecycleBin)
                                {
                                    FileSystem.DeleteDirectory(dir, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                                }
                                else
                                {
                                    Directory.Delete(dir);
                                }
                            }
                            catch (Exception) {
                                ShowMessage("Failed to delete directory: " + dir);
                            }
                        }
                    }
                }
            }

            return(success);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            try {
                var options = DeleteFilesOptions.Parse(args);

                var processor = new DeleteFilesProcessor();
                processor.ProcessFiles(options);
            }
            catch (Exception ex) {
                Console.WriteLine(ex);
                Environment.Exit(1);
            }
        }
Ejemplo n.º 5
0
        private bool IsFileToBeDeleted(string file, DeleteFilesOptions options)
        {
            if (options.Seconds > -1)
            {
                var ftime = File.GetLastWriteTimeUtc(file);
                return(DateTime.UtcNow > ftime.AddSeconds(options.Seconds));
            }

            if (options.Days > -1)
            {
                var ftime = File.GetLastWriteTime(file);
                return(DateTime.Now.Date >= ftime.Date.AddDays(options.Days));
            }

            return(true);
        }
Ejemplo n.º 6
0
        public static DeleteFilesOptions Parse(IEnumerable<string> arguments)
        {
            var parms = new DeleteFilesOptions();

             var options = new OptionSet()
            .Add("r", p => parms.Recursive = true)
            .Add("f", p => parms.RemoveEmptyDirectories = true)
            .Add("y", p => parms.UseRecycleBin = true)
            .Add("d=", d => parms.Days = Convert.ToInt32(d))
            .Add("s=", s => parms.Seconds = Convert.ToInt32(s))
            .Add("help|?", p => ShowHelp());

             var fullpath = options.Parse(arguments);

             if (!fullpath.Any()) {
            Console.WriteLine("pathspec not specified!");
            ShowHelp();
             }

             parms.Path = System.IO.Path.GetDirectoryName(fullpath[0]);
             parms.FileSpec = System.IO.Path.GetFileName(fullpath[0]);

             return parms;
        }