Example #1
0
        public List <FastFileInfo> GetAllFiles(SearchCriteria criteria)
        {
            //string[] fileEntries = null;
            IEnumerable <FastFileInfo> files = null;
            //var directory = new DirectoryInfo(criteria.TargetFolder);
            var searchPattern = criteria.SearchPattern.Equals(string.Empty) ? "*.*" : criteria.SearchPattern;
            var searchOption  = criteria.IncludeSubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            if (criteria.UseDateRange)
            {
                var startTime = new DateTime();
                var endTime   = DateTime.Now;

                if (criteria.DateRangeToday)
                {
                    startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                }
                else if (criteria.DateRangeLast4Hours)
                {
                    startTime = DateTime.Now.AddHours(-4);
                }
                else if (criteria.DateRangeLastHour)
                {
                    startTime = DateTime.Now.AddHours(-1);
                }
                else if (criteria.DateRangeJustNow)
                {
                    startTime = DateTime.Now.AddMinutes(-2);
                }
                else // Use from and to date
                {
                    startTime = criteria.StartTime ?? DateTime.MinValue;
                    endTime   = criteria.EndTime ?? DateTime.MinValue;
                }

                // Source: https://sourceforge.net/projects/fastfileinfo/

                //files = directory.GetFiles(searchPattern, searchOption)
                //    .Where(f => f.LastWriteTime >= startTime && f.LastWriteTime <= endTime);
                files = FastFileInfo.EnumerateFiles(criteria.TargetFolder, searchPattern, searchOption)
                        .Where(f => f.LastWriteTime >= startTime && f.LastWriteTime <= endTime);
            }
            else
            {
                //files = directory.GetFiles(searchPattern, searchOption);
                files = FastFileInfo.EnumerateFiles(criteria.TargetFolder, searchPattern, searchOption);
            }

            return(files.ToList());
        }
Example #2
0
            /// <summary>
            /// Gets <see cref="FastFileInfo"/> for all the files in a directory that match a specific filter.
            /// </summary>
            /// <param name="path">The path to search.</param>
            /// <param name="searchPattern">The search string to match against files in the path. Multiple can be specified separated by the pipe character.</param>
            /// <returns>An list of FastFileInfo objects that match the specified search pattern.</returns>
            /// <exception cref="ArgumentNullException">
            /// <paramref name="path"/> is a null reference (Nothing in VB)
            /// </exception>
            /// <exception cref="ArgumentNullException">
            /// <paramref name="filter"/> is a null reference (Nothing in VB)
            /// </exception>
            public static IList <FastFileInfo> GetFiles(String path, String searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly)
            {
                List <FastFileInfo> list = new List <FastFileInfo>();

                String[]  arr = searchPattern.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                Hashtable ht  = (arr.Length > 1 ? new Hashtable() : null); // don't need to worry about case since it should be consistent

                foreach (String sp in arr)
                {
                    String sp2 = sp.Trim();
                    if (sp2.Length == 0)
                    {
                        continue;
                    }

                    IEnumerable <FastFileInfo> e = EnumerateFiles(path, sp2, searchOption);
                    if (ht == null)
                    {
                        list.AddRange(e);
                    }
                    else
                    {
                        var e2 = e.GetEnumerator();
                        if (ht.Count == 0)
                        {
                            while (e2.MoveNext())
                            {
                                FastFileInfo f = e2.Current;
                                list.Add(f);
                                ht[f.FullName] = f;
                            }
                        }
                        else
                        {
                            while (e2.MoveNext())
                            {
                                FastFileInfo f = e2.Current;
                                if (!ht.Contains(f.FullName))
                                {
                                    list.Add(f);
                                    ht[f.FullName] = f;
                                }
                            }
                        }
                    }
                }

                return(list);
            }