Esempio n. 1
0
        // return true if directory is deleted otherwise false
        //public static bool DeleteEmptyDirectory(string directory, bool deleteOnlySubdirectory = true)
        public static bool DeleteEmptyDirectory(string directory, bool recurse = false)
        {
            if (!zDirectory.Exists(directory))
            {
                return(false);
            }
            Stack <EnumDirectory> directoryStack = new Stack <EnumDirectory>();
            EnumDirectory         enumDirectory  = new EnumDirectory(directory);

            while (true)
            {
                if (enumDirectory.directoryEnum.MoveNext())
                {
                    directoryStack.Push(enumDirectory);

                    enumDirectory = new EnumDirectory(enumDirectory.directoryEnum.Current);
                }
                else
                {
                    if (directoryStack.Count == 0)
                    {
                        //if (!deleteOnlySubdirectory && IsDirectoryEmpty(enumDirectory.directoryInfo))
                        if (recurse && IsDirectoryEmpty(enumDirectory.directoryInfo))
                        {
                            enumDirectory.directoryInfo.Delete();
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    if (IsDirectoryEmpty(enumDirectory.directoryInfo))
                    {
                        enumDirectory.directoryInfo.Delete();
                    }
                    enumDirectory = directoryStack.Pop();
                }
            }
        }
Esempio n. 2
0
 // return true if directory is deleted otherwise false
 //public static bool DeleteEmptyDirectory(string directory, bool deleteOnlySubdirectory = true)
 public static bool DeleteEmptyDirectory(string directory, bool recurse = false)
 {
     if (!zDirectory.Exists(directory))
         return false;
     Stack<EnumDirectory> directoryStack = new Stack<EnumDirectory>();
     EnumDirectory enumDirectory = new EnumDirectory(directory);
     while (true)
     {
         if (enumDirectory.directoryEnum.MoveNext())
         {
             directoryStack.Push(enumDirectory);
             enumDirectory = new EnumDirectory(enumDirectory.directoryEnum.Current);
         }
         else
         {
             if (directoryStack.Count == 0)
             {
                 //if (!deleteOnlySubdirectory && IsDirectoryEmpty(enumDirectory.directoryInfo))
                 if (recurse && IsDirectoryEmpty(enumDirectory.directoryInfo))
                 {
                     enumDirectory.directoryInfo.Delete();
                     return true;
                 }
                 else
                     return false;
             }
             if (IsDirectoryEmpty(enumDirectory.directoryInfo))
                 enumDirectory.directoryInfo.Delete();
             enumDirectory = directoryStack.Pop();
         }
     }
 }
Esempio n. 3
0
        // followDirectoryTree : followDirectoryTree est appelé quand on entre dans un répertoire et quand on en sort
        //                       à l'entrée EnumDirectoryInfo contient les info du répertoire
        //                       à la sortie EnumDirectoryInfo contient uniquement le Level
        // followDirectoryTree exemple EnumerateDirectoriesInfo(@"c:\toto");
        //   entré dans tata EnumDirectoryInfo = Directory: "c:\toto\tata" SubDirectory: "tata" Level: 1
        //   entré dans tutu EnumDirectoryInfo = Directory: "c:\toto\tata\tutu" SubDirectory: "tutu" Level: 2
        //   sortie de tutu EnumDirectoryInfo = Directory: null SubDirectory: null Level: 2
        //   sortie de tata EnumDirectoryInfo = Directory: null SubDirectory: null Level: 1
        public static IEnumerable <EnumDirectoryInfo> EnumerateDirectoriesInfo(string directory, string pattern = null,
                                                                               //Func<EnumDirectoryInfo, EnumDirectoryFilter> directoryFilter = null,
                                                                               IEnumerable <Func <EnumDirectoryInfo, EnumDirectoryFilter> > directoryFilters = null,
                                                                               //Action<EnumDirectoryInfo> followDirectoryTree = null,
                                                                               IEnumerable <Action <EnumDirectoryInfo> > followDirectoryTrees = null,
                                                                               bool getSubDirectoryNumber = false)
        {
            if (!zDirectory.Exists(directory))
            {
                yield break;
            }
            Stack <EnumDirectory> directoryStack      = new Stack <EnumDirectory>();
            EnumDirectory         enumDirectory       = new EnumDirectory(directory, pattern);
            EnumDirectoryFilter   enumDirectoryFilter = new EnumDirectoryFilter {
                Select = true, RecurseSubDirectory = true
            };
            int l     = directory.Length + 1;
            int level = 1;

            while (true)
            {
                if (enumDirectory.directoryEnum.MoveNext())
                {
                    DirectoryInfo     directoryInfo     = enumDirectory.directoryEnum.Current;
                    EnumDirectoryInfo enumDirectoryInfo = new EnumDirectoryInfo
                    {
                        Directory = directoryInfo.FullName, SubDirectory = directoryInfo.FullName.Substring(l), Name = directoryInfo.Name, Level = level
                    };

                    if (getSubDirectoryNumber)
                    {
                        FileNumber directoryNumberInfo = FileNumber.GetFileNumber(enumDirectoryInfo.Name);
                        enumDirectoryInfo.Name   = directoryNumberInfo.BasePath;
                        enumDirectoryInfo.Number = directoryNumberInfo.Number;
                    }

                    //if (directoryFilter != null)
                    //    enumDirectoryFilter = directoryFilter(enumDirectoryInfo);
                    if (directoryFilters != null)
                    {
                        enumDirectoryFilter = EvaluateDirectoryFilters(directoryFilters, enumDirectoryInfo);
                    }

                    if (enumDirectoryFilter.Select)
                    {
                        //if (followDirectoryTree != null)
                        //    followDirectoryTree(enumDirectoryInfo);
                        if (followDirectoryTrees != null)
                        {
                            FollowDirectoryTrees(followDirectoryTrees, enumDirectoryInfo);
                        }

                        //if (getSubDirectoryNumber)
                        //{
                        //    FilenameNumberInfo directoryNumberInfo = FilenameNumberInfo.GetFilenameNumberInfo(enumDirectoryInfo.SubDirectory);
                        //    //enumDirectoryInfo.SubDirectory = directoryNumberInfo.BaseFilename;
                        //    enumDirectoryInfo.SubDirectory = directoryNumberInfo.BasePath;
                        //    enumDirectoryInfo.Number = directoryNumberInfo.Number;
                        //}
                        yield return(enumDirectoryInfo);

                        //if (!enumDirectoryFilter.RecurseSubDirectory && followDirectoryTree != null)
                        //    followDirectoryTree(new EnumDirectoryInfo { Level = level });
                        if (!enumDirectoryFilter.RecurseSubDirectory && followDirectoryTrees != null)
                        {
                            FollowDirectoryTrees(followDirectoryTrees, new EnumDirectoryInfo {
                                Level = level
                            });
                        }
                    }

                    if (enumDirectoryFilter.RecurseSubDirectory)
                    {
                        //if (!enumDirectoryFilter.Select && followDirectoryTree != null)
                        //    followDirectoryTree(enumDirectoryInfo);
                        if (!enumDirectoryFilter.Select && followDirectoryTrees != null)
                        {
                            FollowDirectoryTrees(followDirectoryTrees, enumDirectoryInfo);
                        }

                        directoryStack.Push(enumDirectory);
                        enumDirectory = new EnumDirectory(enumDirectory.directoryEnum.Current, pattern);
                        level++;
                    }
                }
                else
                {
                    if (directoryStack.Count == 0)
                    {
                        break;
                    }
                    enumDirectory = directoryStack.Pop();
                    level--;

                    //if (followDirectoryTree != null)
                    //    followDirectoryTree(new EnumDirectoryInfo { Level = level });
                    if (followDirectoryTrees != null)
                    {
                        FollowDirectoryTrees(followDirectoryTrees, new EnumDirectoryInfo {
                            Level = level
                        });
                    }
                }
            }
        }
Esempio n. 4
0
        // followDirectoryTree : followDirectoryTree est appelé quand on entre dans un répertoire et quand on en sort
        //                       à l'entrée EnumDirectoryInfo contient les info du répertoire
        //                       à la sortie EnumDirectoryInfo contient uniquement le Level
        // followDirectoryTree exemple EnumerateDirectoriesInfo(@"c:\toto");
        //   entré dans tata EnumDirectoryInfo = Directory: "c:\toto\tata" SubDirectory: "tata" Level: 1
        //   entré dans tutu EnumDirectoryInfo = Directory: "c:\toto\tata\tutu" SubDirectory: "tutu" Level: 2
        //   sortie de tutu EnumDirectoryInfo = Directory: null SubDirectory: null Level: 2
        //   sortie de tata EnumDirectoryInfo = Directory: null SubDirectory: null Level: 1
        public static IEnumerable<EnumDirectoryInfo> EnumerateDirectoriesInfo(string directory, string pattern = null,
            //Func<EnumDirectoryInfo, EnumDirectoryFilter> directoryFilter = null,
            IEnumerable<Func<EnumDirectoryInfo, EnumDirectoryFilter>> directoryFilters = null,
            //Action<EnumDirectoryInfo> followDirectoryTree = null,
            IEnumerable<Action<EnumDirectoryInfo>> followDirectoryTrees = null,
            bool getSubDirectoryNumber = false)
        {
            if (!zDirectory.Exists(directory))
                yield break;
            Stack<EnumDirectory> directoryStack = new Stack<EnumDirectory>();
            EnumDirectory enumDirectory = new EnumDirectory(directory, pattern);
            EnumDirectoryFilter enumDirectoryFilter = new EnumDirectoryFilter { Select = true, RecurseSubDirectory = true };
            int l = directory.Length + 1;
            int level = 1;
            while (true)
            {
                if (enumDirectory.directoryEnum.MoveNext())
                {
                    DirectoryInfo directoryInfo = enumDirectory.directoryEnum.Current;
                    EnumDirectoryInfo enumDirectoryInfo = new EnumDirectoryInfo
                        { Directory = directoryInfo.FullName, SubDirectory = directoryInfo.FullName.Substring(l), Name = directoryInfo.Name, Level = level };

                    if (getSubDirectoryNumber)
                    {
                        FilenameNumberInfo directoryNumberInfo = FilenameNumberInfo.GetFilenameNumberInfo(enumDirectoryInfo.Name);
                        enumDirectoryInfo.Name = directoryNumberInfo.BasePath;
                        enumDirectoryInfo.Number = directoryNumberInfo.Number;
                    }

                    //if (directoryFilter != null)
                    //    enumDirectoryFilter = directoryFilter(enumDirectoryInfo);
                    if (directoryFilters != null)
                        enumDirectoryFilter = EvaluateDirectoryFilters(directoryFilters, enumDirectoryInfo);

                    if (enumDirectoryFilter.Select)
                    {
                        //if (followDirectoryTree != null)
                        //    followDirectoryTree(enumDirectoryInfo);
                        if (followDirectoryTrees != null)
                            FollowDirectoryTrees(followDirectoryTrees, enumDirectoryInfo);

                        //if (getSubDirectoryNumber)
                        //{
                        //    FilenameNumberInfo directoryNumberInfo = FilenameNumberInfo.GetFilenameNumberInfo(enumDirectoryInfo.SubDirectory);
                        //    //enumDirectoryInfo.SubDirectory = directoryNumberInfo.BaseFilename;
                        //    enumDirectoryInfo.SubDirectory = directoryNumberInfo.BasePath;
                        //    enumDirectoryInfo.Number = directoryNumberInfo.Number;
                        //}
                        yield return enumDirectoryInfo;

                        //if (!enumDirectoryFilter.RecurseSubDirectory && followDirectoryTree != null)
                        //    followDirectoryTree(new EnumDirectoryInfo { Level = level });
                        if (!enumDirectoryFilter.RecurseSubDirectory && followDirectoryTrees != null)
                            FollowDirectoryTrees(followDirectoryTrees, new EnumDirectoryInfo { Level = level });
                    }

                    if (enumDirectoryFilter.RecurseSubDirectory)
                    {
                        //if (!enumDirectoryFilter.Select && followDirectoryTree != null)
                        //    followDirectoryTree(enumDirectoryInfo);
                        if (!enumDirectoryFilter.Select && followDirectoryTrees != null)
                            FollowDirectoryTrees(followDirectoryTrees, enumDirectoryInfo);

                        directoryStack.Push(enumDirectory);
                        enumDirectory = new EnumDirectory(enumDirectory.directoryEnum.Current, pattern);
                        level++;
                    }
                }
                else
                {
                    if (directoryStack.Count == 0)
                        break;
                    enumDirectory = directoryStack.Pop();
                    level--;

                    //if (followDirectoryTree != null)
                    //    followDirectoryTree(new EnumDirectoryInfo { Level = level });
                    if (followDirectoryTrees != null)
                        FollowDirectoryTrees(followDirectoryTrees, new EnumDirectoryInfo { Level = level });
                }
            }
        }