Advertisement
genBTC

Code C# FileTime Enumerate Directories / Files for Date Time

Sep 3rd, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.41 KB | None | 0 0
  1.         private IEnumerable<String> GetFilesAndDirsSafely(string path, string filePattern, bool recurse)
  2.         {
  3.             IEnumerable<String> emptyList = new string[0];
  4.  
  5.             if (File.Exists(path))
  6.                 return new[] { path };
  7.  
  8.             if (!Directory.Exists(path))
  9.                 return emptyList;
  10.  
  11.             var topDirectory = new DirectoryInfo(path);
  12.  
  13.             // Enumerate the files just in the top directory.
  14.             var files = topDirectory.EnumerateFiles(filePattern);
  15.             var fileInfos = files as FileInfo[] ?? files.ToArray();
  16.             int filesLength = fileInfos.Length;
  17.             IEnumerable<String> topDirEnum = Enumerable.Repeat(path, 1);
  18.             IEnumerable<String> filesList = Enumerable.Range(0, filesLength).Select(i =>
  19.                 {
  20.                     string filename = null;
  21.                     try
  22.                     {
  23.                         var file = fileInfos.ElementAt(i);
  24.                         filename = file.FullName;
  25.                     } catch (UnauthorizedAccessException)
  26.                     { } catch (FileNotFoundException)
  27.                     { } catch (InvalidOperationException)
  28.                     { }
  29.                     return filename;
  30.                 })
  31.                 .Where(i => null != i);
  32.  
  33.             if (!recurse)
  34.                 return topDirEnum.Concat(filesList);
  35.  
  36.             var dirs = topDirectory.EnumerateDirectories("*");
  37.             var directoryInfos = dirs as DirectoryInfo[] ?? dirs.ToArray();
  38.             int dirsLength = directoryInfos.Length;
  39.             IEnumerable<String> dirsList = Enumerable.Range(0, dirsLength).SelectMany(i =>
  40.             {
  41.                 try
  42.                 {
  43.                     var dir = directoryInfos.ElementAt(i);
  44.                     string dirname = dir.FullName;
  45.                     return GetFilesAndDirsSafely(dirname, filePattern, recurse);
  46.                 } catch (UnauthorizedAccessException)
  47.                 { } catch (DirectoryNotFoundException)
  48.                 { } catch (InvalidOperationException)
  49.                 { }
  50.                 return emptyList;
  51.             });
  52.  
  53.             return topDirEnum.Concat(filesList.Concat(dirsList));
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Mode 3: Process One directory, Process 2nd Dir. with recursive sub-directory support. Calls SetFileDateTime()
  58.         /// (Only adds to the confirm list, Form 2 will actually write changes).
  59.         /// </summary>
  60.         /// <param name="targetPath">Full path to the targetPath directory</param>
  61.         /// <param name="comparePath">Full path to the comparePath directory</param>
  62.         /// req, checkBox_Recurse.Checked, checkBoxShouldFiles.Checked
  63.         internal void RecurseSubDirectoryMode3(string targetPath, string comparePath)
  64.         {
  65.             //no point continuing if we have nothing matching to compare to.
  66.             if (!Directory.Exists(comparePath))
  67.                 return;
  68.             if (!comparePath.EndsWith(SharedHelper.SeperatorString))
  69.                 comparePath += SharedHelper.Seperator;
  70.  
  71.             // Take a snapshot of the paths of the file system.  Makes an IEnumerable.
  72.             IEnumerable<string> destFileList = GetFilesAndDirsSafely(targetPath, "*", true);
  73.             IEnumerable<string> srcFileList = GetFilesAndDirsSafely(comparePath, "*", true);
  74.             // Find the common files. It produces a sequence and doesn't execute until the foreach statement.  
  75.             IEnumerable<string> queryCommonFiles = srcFileList.Intersect(destFileList, SharedHelper.explorerStringEqualityComparer(targetPath, comparePath));
  76.  
  77.             foreach (string f in queryCommonFiles)
  78.             {
  79.                 NameDateQuick srcfiletime = GetCmaTimesFromFilesystem(f);
  80.                 string nameChanged = f.Replace(comparePath, targetPath);
  81.                 bool isDirectory = Directory.Exists(nameChanged);
  82.                 SkipOrAddFile(nameChanged, isDirectory);
  83.  
  84.                 var currentobject = new NameDateObjListViewVMdl(srcfiletime) { Name = nameChanged, FileOrDirType = SharedHelper.Bool2Int(isDirectory) };
  85.                 //If Checkbox is selected: writes each time time to the date attribute that was radiobutton selected.
  86.                 StoreDateByCMACheckboxes(currentobject);
  87.  
  88.                 var item = new NameDateObj(currentobject.Converter());
  89.  
  90.                 FilestoConfirmList.Add(item);
  91.             }
  92.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement