fork download
  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Text;
  8.  
  9. using System.IO;
  10.  
  11.  
  12.  
  13. namespace QueryCompareTwoDirs
  14.  
  15. {
  16.  
  17. class CompareDirs
  18.  
  19. {
  20.  
  21.  
  22.  
  23. static void Main(string[] args)
  24.  
  25. {
  26.  
  27.  
  28.  
  29. // Create two identical or different temporary folders
  30.  
  31. // on a local drive and change these file paths.
  32.  
  33. string pathA = @"\\ubuntu\www";
  34.  
  35. string pathB = @"\\WIN7X64ULT\j$\www";
  36.  
  37. int LengthPathA = pathA.Length;
  38.  
  39. int LengthPathB = pathB.Length;
  40.  
  41.  
  42.  
  43. Console.WriteLine("Pause 10 seconds");
  44.  
  45. System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(10).TotalMilliseconds);
  46.  
  47. Console.WriteLine("Done pausing");
  48.  
  49.  
  50.  
  51. System.IO.DirectoryInfo dirfile1 = new System.IO.DirectoryInfo(pathA);
  52.  
  53. IEnumerable<System.IO.DirectoryInfo> dirlist1 = dirfile1.GetDirectories("*.*", System.IO.SearchOption.AllDirectories);
  54.  
  55. Console.WriteLine("Step 4 Dir path A");
  56.  
  57.  
  58.  
  59.  
  60.  
  61. Console.WriteLine("Pause 5 seconds");
  62.  
  63. System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(5).TotalMilliseconds);
  64.  
  65. Console.WriteLine("Done pausing");
  66.  
  67.  
  68.  
  69. // These lines find all the files
  70.  
  71. // Take a snapshot of the file system.
  72.  
  73. System.IO.DirectoryInfo file1 = new System.IO.DirectoryInfo(pathA);
  74.  
  75. IEnumerable<System.IO.FileInfo> list1 = file1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  76.  
  77. Console.WriteLine("Step 1 files path A");
  78.  
  79.  
  80.  
  81. using (System.IO.StreamWriter afile1 = new System.IO.StreamWriter(@"E:\compare\afile1.txt"))
  82.  
  83. foreach(var a in list1)
  84.  
  85. {
  86.  
  87. afile1.WriteLine(a.FullName.ToString());
  88.  
  89. }
  90.  
  91.  
  92.  
  93. Console.WriteLine("Pause 5 seconds");
  94.  
  95. System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(5).TotalMilliseconds);
  96.  
  97. Console.WriteLine("Done pausing");
  98.  
  99.  
  100.  
  101. System.IO.DirectoryInfo file2 = new System.IO.DirectoryInfo(pathB);
  102.  
  103. IEnumerable<System.IO.FileInfo> list2 = file2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  104.  
  105. Console.WriteLine("Step 2 files path B");
  106.  
  107.  
  108.  
  109. using (System.IO.StreamWriter bfile2 = new System.IO.StreamWriter(@"E:\compare\bfile2.txt"))
  110.  
  111. foreach (var a in list2)
  112.  
  113. {
  114.  
  115. bfile2.WriteLine(a.FullName.ToString());
  116.  
  117. }
  118.  
  119.  
  120.  
  121. Console.WriteLine("Pause 5 seconds");
  122.  
  123. System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(5).TotalMilliseconds);
  124.  
  125. Console.WriteLine("Done pausing");
  126.  
  127.  
  128.  
  129. System.IO.DirectoryInfo dirfile2 = new System.IO.DirectoryInfo(pathB);
  130.  
  131. IEnumerable<System.IO.DirectoryInfo> dirlist2 = dirfile2.GetDirectories("*.*", System.IO.SearchOption.AllDirectories);
  132.  
  133. Console.WriteLine("Step 3 Dir path B");
  134.  
  135.  
  136.  
  137. using (System.IO.StreamWriter bdir2 = new System.IO.StreamWriter(@"E:\compare\bdir2.txt"))
  138.  
  139. foreach (var a in dirlist2)
  140.  
  141. {
  142.  
  143. string bd2 = a.Name;
  144.  
  145. bdir2.WriteLine(bd2);
  146.  
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. using (System.IO.StreamWriter adir2 = new System.IO.StreamWriter(@"E:\compare\adir2.txt"))
  156.  
  157. foreach (var a in dirlist1)
  158.  
  159. {
  160.  
  161. string ad2 = a.Name;
  162.  
  163. adir2.WriteLine(ad2);
  164.  
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171. // These lines find all the directories
  172.  
  173.  
  174.  
  175.  
  176.  
  177. // Take a snapshot of the directory system.
  178.  
  179.  
  180.  
  181.  
  182.  
  183. /*
  184.  
  185.   * A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
  186.  
  187.  
  188.  
  189.   Additional information: The specified network name is no longer available.
  190.  
  191.  
  192.  
  193.   If there is a handler for this exception, the program may be safely continued.
  194.  
  195.   *
  196.  
  197.   */
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\compare\dirlist1.txt"))
  218.  
  219. foreach (var x in list1)
  220.  
  221. {
  222.  
  223. file.WriteLine(x.ToString());
  224.  
  225. }
  226.  
  227.  
  228.  
  229. using (System.IO.StreamWriter filelist1 = new System.IO.StreamWriter(@"E:\compare\filelist1.txt"))
  230.  
  231. foreach (var x in list1)
  232.  
  233. {
  234.  
  235. filelist1.WriteLine(x.ToString());
  236.  
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243. //A custom file comparer defined below
  244.  
  245. FileCompare myFileCompare = new FileCompare();
  246.  
  247.  
  248.  
  249. // This query determines whether the two folders contain
  250.  
  251. // identical file lists, based on the custom file comparer
  252.  
  253. // that is defined in the FileCompare class.
  254.  
  255. // The query executes immediately because it returns a bool.
  256.  
  257. bool areIdentical = list1.SequenceEqual(list2, myFileCompare);
  258.  
  259.  
  260.  
  261. if (areIdentical == true)
  262.  
  263. {
  264.  
  265. Console.WriteLine("the two folders are the same");
  266.  
  267. }
  268.  
  269. else
  270.  
  271. {
  272.  
  273. Console.WriteLine("The two folders are not the same");
  274.  
  275. }
  276.  
  277.  
  278.  
  279. // Find the common files. It produces a sequence and doesn't
  280.  
  281. // execute until the foreach statement.
  282.  
  283. var queryCommonFiles = list1.Intersect(list2, myFileCompare);
  284.  
  285.  
  286.  
  287. if (queryCommonFiles.Count() > 0)
  288.  
  289. {
  290.  
  291. Console.WriteLine("The following files are in both folders:");
  292.  
  293.  
  294.  
  295. foreach (var v in queryCommonFiles)
  296.  
  297. {
  298.  
  299. Console.WriteLine(v.FullName); //shows which items end up in result list
  300.  
  301. }
  302.  
  303.  
  304.  
  305. }
  306.  
  307. else
  308.  
  309. {
  310.  
  311. Console.WriteLine("There are no common files in the two folders.");
  312.  
  313. }
  314.  
  315.  
  316.  
  317. // Find the set difference between the two folders.
  318.  
  319. // For this example we only check one way.
  320.  
  321.  
  322.  
  323.  
  324.  
  325. var queryDirListOnly = (from dirfile in dirlist1 select dirfile);
  326.  
  327. using (System.IO.StreamWriter filedirlist1 = new System.IO.StreamWriter(@"E:\compare\queryDirlist1.txt"))
  328.  
  329. foreach (var v in queryDirListOnly)
  330.  
  331. {
  332.  
  333. var FinalDirCut = pathB + v.FullName.Substring(LengthPathA);
  334.  
  335. filedirlist1.WriteLine(FinalDirCut);
  336.  
  337.  
  338.  
  339. try
  340.  
  341. {
  342.  
  343.  
  344.  
  345. System.IO.Directory.CreateDirectory(FinalDirCut);
  346.  
  347. Console.Write(FinalDirCut);
  348.  
  349. Console.WriteLine(" Directory Created");
  350.  
  351. }
  352.  
  353. catch (IOException)
  354.  
  355. { }
  356.  
  357.  
  358.  
  359. }
  360.  
  361.  
  362.  
  363. var queryList1Only = (from file in list1
  364.  
  365. select file).Except(list2, myFileCompare);
  366.  
  367.  
  368.  
  369. Console.WriteLine("The following files are in list1 but not list2:");
  370.  
  371.  
  372.  
  373. using (System.IO.StreamWriter filelist1 = new System.IO.StreamWriter(@"E:\compare\filelist1.txt"))
  374.  
  375. foreach (var x in queryList1Only)
  376.  
  377. {
  378.  
  379. Console.WriteLine(x.FullName);
  380.  
  381. filelist1.WriteLine(x.FullName.ToString());
  382.  
  383. }
  384.  
  385. using (System.IO.StreamWriter FinalWrite = new System.IO.StreamWriter(@"E:\compare\FinalWrite.csv", true))
  386.  
  387.  
  388.  
  389. foreach (var v in queryList1Only)
  390.  
  391. {
  392.  
  393. Console.WriteLine(v.FullName);
  394.  
  395. String SourceSub = v.DirectoryName.Substring(LengthPathA);
  396.  
  397. Console.WriteLine(SourceSub);
  398.  
  399. Console.WriteLine(v.Directory);
  400.  
  401. Console.WriteLine(v.Name);
  402.  
  403. String Source = v.FullName;
  404.  
  405.  
  406.  
  407. String Dest = pathB + SourceSub + "\\" + v.Name;
  408.  
  409. Console.WriteLine(Source);
  410.  
  411. Console.WriteLine(Dest);
  412.  
  413. Console.WriteLine("");
  414.  
  415.  
  416.  
  417. String DirToCreate = v.DirectoryName;
  418.  
  419. Console.Write(DirToCreate);
  420.  
  421. Console.WriteLine(" Does Not Exist in pathB");
  422.  
  423. Console.WriteLine("");
  424.  
  425.  
  426.  
  427. FinalWrite.WriteLine("{0},{1}", Source, Dest);
  428.  
  429.  
  430.  
  431.  
  432.  
  433. try
  434.  
  435. {
  436.  
  437. File.Copy(Source, Dest, true);
  438.  
  439. }
  440.  
  441. catch (IOException copyError)
  442.  
  443. {
  444.  
  445. Console.WriteLine(copyError.Message);
  446.  
  447. }
  448.  
  449.  
  450.  
  451.  
  452.  
  453. Console.Write(Source);
  454.  
  455. Console.WriteLine(" Copied to ");
  456.  
  457. Console.WriteLine(Dest);
  458.  
  459.  
  460.  
  461. }
  462.  
  463.  
  464.  
  465. System.IO.DirectoryInfo newfile2 = new System.IO.DirectoryInfo(pathB);
  466.  
  467. IEnumerable<System.IO.FileInfo> newlist2 = newfile2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  468.  
  469.  
  470.  
  471. var newqueryList1Only = (from file in list1
  472.  
  473. select file).Except(newlist2, myFileCompare);
  474.  
  475.  
  476.  
  477. Console.WriteLine("");
  478.  
  479. Console.WriteLine("Compare after copying complete");
  480.  
  481. Console.WriteLine("The following files are in list1 but not list2:");
  482.  
  483.  
  484.  
  485. using (System.IO.StreamWriter newfilelist1 = new System.IO.StreamWriter(@"E:\compare\newfilelist1.txt"))
  486.  
  487. foreach (var x in newqueryList1Only)
  488.  
  489. {
  490.  
  491. newfilelist1.WriteLine(x.FullName.ToString());
  492.  
  493. }
  494.  
  495.  
  496.  
  497. // Keep the console window open in debug mode.
  498.  
  499. Console.WriteLine("Press any key to exit.");
  500.  
  501. Console.ReadKey();
  502.  
  503.  
  504.  
  505.  
  506.  
  507. }
  508.  
  509. }
  510.  
  511.  
  512.  
  513. // This implementation defines a very simple comparison
  514.  
  515. // between two FileInfo objects. It only compares the name
  516.  
  517. // of the files being compared and their length in bytes.
  518.  
  519. class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
  520.  
  521. {
  522.  
  523. public FileCompare() { }
  524.  
  525.  
  526.  
  527. public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
  528.  
  529. {
  530.  
  531. return (f1.Name == f2.Name &&
  532.  
  533. f1.Length == f2.Length);
  534.  
  535. }
  536.  
  537.  
  538.  
  539. // Return a hash that reflects the comparison criteria. According to the
  540.  
  541. // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
  542.  
  543. // also be equal. Because equality as defined here is a simple value equality, not
  544.  
  545. // reference identity, it is possible that two or more objects will produce the same
  546.  
  547. // hash code.
  548.  
  549. public int GetHashCode(System.IO.FileInfo fi)
  550.  
  551. {
  552.  
  553. string s = String.Format("{0}{1}", fi.Name, fi.Length);
  554.  
  555. return s.GetHashCode();
  556.  
  557. }
  558.  
  559. }
  560.  
  561. }
  562.  
  563.  
Runtime error #stdin #stdout #stderr 0.05s 28016KB
stdin
Standard input is empty
stdout
Pause 10 seconds
Done pausing
stderr
Unhandled Exception:
System.IO.DirectoryNotFoundException: Directory '/home/lwqQou/\\ubuntu\www' not found.
  at System.IO.Directory.ValidateDirectoryListing (System.String path, System.String searchPattern, System.Boolean& stop) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetFileSystemEntries (System.String path, System.String searchPattern, FileAttributes mask, FileAttributes attrs) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetDirectories (System.String path, System.String searchPattern) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetDirectoriesRecurse (System.String path, System.String searchPattern, System.Collections.Generic.List`1 all) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetDirectories (System.String path, System.String searchPattern, SearchOption searchOption) [0x00000] in <filename unknown>:0 
  at System.IO.DirectoryInfo.GetDirectories (System.String searchPattern, SearchOption searchOption) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.DirectoryInfo:GetDirectories (string,System.IO.SearchOption)
  at QueryCompareTwoDirs.CompareDirs.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.DirectoryNotFoundException: Directory '/home/lwqQou/\\ubuntu\www' not found.
  at System.IO.Directory.ValidateDirectoryListing (System.String path, System.String searchPattern, System.Boolean& stop) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetFileSystemEntries (System.String path, System.String searchPattern, FileAttributes mask, FileAttributes attrs) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetDirectories (System.String path, System.String searchPattern) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetDirectoriesRecurse (System.String path, System.String searchPattern, System.Collections.Generic.List`1 all) [0x00000] in <filename unknown>:0 
  at System.IO.Directory.GetDirectories (System.String path, System.String searchPattern, SearchOption searchOption) [0x00000] in <filename unknown>:0 
  at System.IO.DirectoryInfo.GetDirectories (System.String searchPattern, SearchOption searchOption) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.IO.DirectoryInfo:GetDirectories (string,System.IO.SearchOption)
  at QueryCompareTwoDirs.CompareDirs.Main (System.String[] args) [0x00000] in <filename unknown>:0