예제 #1
0
        public static List <FileInfo> FilterFileListBySearchOptions(List <FileInfo> files, FileSearch fileSearchOptions, out string error)
        {
            error = null;
            var filteredFiles = new List <FileInfo>();

            try
            {
                foreach (var file in files)
                {
                    var fFileName  = Path.GetFileNameWithoutExtension(file.Name.ToLower().Trim());
                    var sFileName  = fileSearchOptions.FileName.ToLower().Trim();
                    var extensions = fileSearchOptions.Extensions.ToLower().Trim().Split(',').ToList();

                    if (!ApiManager.SearchStringFound(sFileName, fFileName))
                    {
                        continue;
                    }

                    var extFound = false;
                    foreach (var ext in extensions)
                    {
                        if (ApiManager.SearchStringFound(ext.Trim(), Path.GetExtension(file.Name).ToLower().Trim()))
                        {
                            extFound = true; break;
                        }
                    }
                    if (!extFound)
                    {
                        continue;
                    }

                    if (file.LastWriteTime < fileSearchOptions.DateModifiedStart || file.LastWriteTime > fileSearchOptions.DateModifiedEnd ||
                        file.Length < fileSearchOptions.FileSizeStart || file.Length > fileSearchOptions.FileSizeEnd)
                    {
                        continue;
                    }

                    if (!ApiManager.SearchStringFound(fileSearchOptions.Path, file.DirectoryName))
                    {
                        continue;
                    }

                    filteredFiles.Add(file);
                }

                if (!fileSearchOptions.InverseResults)
                {
                    return(filteredFiles);
                }
                else
                {
                    return(files.Except(filteredFiles).ToList());
                }
            }
            catch (Exception e)
            {
                error = e.Message;
                logger.Fatal(e);
                return(null);
            }
        }
예제 #2
0
        public static bool FindAndReplaceOnFullPath(List <FileInfo> files, string inString, string outString, out string error, out List <string> newSearchDirectories)
        {
            try
            {
                error = "";
                List <string> directories = new List <string>();
                newSearchDirectories = new List <string>();
                var drives = GetDriveInformation().Select(x => x.Name).Select(x => x.ToLower()).ToList();

                foreach (var file in files)
                {
                    if (!directories.Contains(file.Directory.FullName))
                    {
                        directories.Add(file.Directory.FullName);
                    }
                }

                foreach (var directory in directories)
                {
                    List <string> preChangePathparts  = new List <string>();
                    List <string> postChangePathparts = new List <string>();
                    var           dir = new DirectoryInfo(directory);

                    if (dir != null && !string.IsNullOrEmpty(dir.Name) && !string.IsNullOrEmpty(dir.FullName))
                    {
                        var dirs = dir.FullName.Split(Path.DirectorySeparatorChar).ToList();

                        for (int i = 0; i < dirs.Count; i++)
                        {
                            if (ApiManager.SearchStringFound(inString, dirs[i]) && !drives.Select(x => x.Contains(inString.ToLower())).Any())
                            {
                                preChangePathparts.Add(dirs[i]);
                                dirs[i] = dirs[i].Replace(inString, outString).Trim();
                                postChangePathparts.Add(dirs[i]);
                                var prePath  = string.Join(Path.DirectorySeparatorChar.ToString(), preChangePathparts);
                                var postPath = string.Join(Path.DirectorySeparatorChar.ToString(), postChangePathparts);
                                if (!Directory.Exists(postPath))
                                {
                                    Directory.Move(prePath, postPath);
                                }
                                preChangePathparts[i] = dirs[i];
                            }
                            else
                            {
                                preChangePathparts.Add(dirs[i]);
                                postChangePathparts.Add(dirs[i]);
                            }
                        }

                        newSearchDirectories.Add(string.Join(Path.DirectorySeparatorChar.ToString(), postChangePathparts));
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
                error = ex.Message;
                newSearchDirectories = null;
                return(false);
            }
        }