Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private IEnumerable<String> GetFilesAndDirsSafely(string path, string filePattern, bool recurse)
- {
- IEnumerable<String> emptyList = new string[0];
- if (File.Exists(path))
- return new[] { path };
- if (!Directory.Exists(path))
- return emptyList;
- var topDirectory = new DirectoryInfo(path);
- // Enumerate the files just in the top directory.
- var files = topDirectory.EnumerateFiles(filePattern);
- var fileInfos = files as FileInfo[] ?? files.ToArray();
- int filesLength = fileInfos.Length;
- IEnumerable<String> topDirEnum = Enumerable.Repeat(path, 1);
- IEnumerable<String> filesList = Enumerable.Range(0, filesLength).Select(i =>
- {
- string filename = null;
- try
- {
- var file = fileInfos.ElementAt(i);
- filename = file.FullName;
- } catch (UnauthorizedAccessException)
- { } catch (FileNotFoundException)
- { } catch (InvalidOperationException)
- { }
- return filename;
- })
- .Where(i => null != i);
- if (!recurse)
- return topDirEnum.Concat(filesList);
- var dirs = topDirectory.EnumerateDirectories("*");
- var directoryInfos = dirs as DirectoryInfo[] ?? dirs.ToArray();
- int dirsLength = directoryInfos.Length;
- IEnumerable<String> dirsList = Enumerable.Range(0, dirsLength).SelectMany(i =>
- {
- try
- {
- var dir = directoryInfos.ElementAt(i);
- string dirname = dir.FullName;
- return GetFilesAndDirsSafely(dirname, filePattern, recurse);
- } catch (UnauthorizedAccessException)
- { } catch (DirectoryNotFoundException)
- { } catch (InvalidOperationException)
- { }
- return emptyList;
- });
- return topDirEnum.Concat(filesList.Concat(dirsList));
- }
- /// <summary>
- /// Mode 3: Process One directory, Process 2nd Dir. with recursive sub-directory support. Calls SetFileDateTime()
- /// (Only adds to the confirm list, Form 2 will actually write changes).
- /// </summary>
- /// <param name="targetPath">Full path to the targetPath directory</param>
- /// <param name="comparePath">Full path to the comparePath directory</param>
- /// req, checkBox_Recurse.Checked, checkBoxShouldFiles.Checked
- internal void RecurseSubDirectoryMode3(string targetPath, string comparePath)
- {
- //no point continuing if we have nothing matching to compare to.
- if (!Directory.Exists(comparePath))
- return;
- if (!comparePath.EndsWith(SharedHelper.SeperatorString))
- comparePath += SharedHelper.Seperator;
- // Take a snapshot of the paths of the file system. Makes an IEnumerable.
- IEnumerable<string> destFileList = GetFilesAndDirsSafely(targetPath, "*", true);
- IEnumerable<string> srcFileList = GetFilesAndDirsSafely(comparePath, "*", true);
- // Find the common files. It produces a sequence and doesn't execute until the foreach statement.
- IEnumerable<string> queryCommonFiles = srcFileList.Intersect(destFileList, SharedHelper.explorerStringEqualityComparer(targetPath, comparePath));
- foreach (string f in queryCommonFiles)
- {
- NameDateQuick srcfiletime = GetCmaTimesFromFilesystem(f);
- string nameChanged = f.Replace(comparePath, targetPath);
- bool isDirectory = Directory.Exists(nameChanged);
- SkipOrAddFile(nameChanged, isDirectory);
- var currentobject = new NameDateObjListViewVMdl(srcfiletime) { Name = nameChanged, FileOrDirType = SharedHelper.Bool2Int(isDirectory) };
- //If Checkbox is selected: writes each time time to the date attribute that was radiobutton selected.
- StoreDateByCMACheckboxes(currentobject);
- var item = new NameDateObj(currentobject.Converter());
- FilestoConfirmList.Add(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement