How to call the List method?

public static void SearchAllFiles() { List<String> files = new List<String>(); String[] extensions = new String[] { "*.jpg", "*.txt", "*.asp" }; foreach (String extension in extensions) { String[] files = Directory.GetFiles(path, found, SearchOption.AllDirectories); foreach (String file in files) files.Add(file); } } 

And I can’t figure out where to paste the file copy:

  File.Copy(path, path + Path.GetFileName(path)); 
  • one
    And why a cycle on extenions? It depends on what point you want to copy the files there and paste. And what is the variable found? Directory.GetFiles (path, pattern, SearchOption.AllDirectories) will not return all files from subdirectories to you. - Yury Bakharev

2 answers 2

First we will make a method for searching files by mask. Works with .NET 4.0 and faster than Directory.GetFiles :

 public static IEnumerable<string> nGetFiles(string path, string searchPatternExpression = "", SearchOption searchOption = SearchOption.AllDirectories) { Regex reSearchPattern = new Regex(searchPatternExpression); return Directory.EnumerateFiles(path, "*", searchOption).Where(file => reSearchPattern.IsMatch(Path.GetFileName(file))); } 

Now so:

 public static void SearchAllFiles() { //Π˜ΡΠΊΠΎΠΌΡ‹Π΅ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ: string LookForExt = "\.jpg|\.txt|\.asp"; //ΠŸΡƒΡ‚ΠΈ ΠΏΠ°ΠΏΠΎΠΊ источника ΠΈ ΠΏΡ€ΠΈΡ‘ΠΌΠ½ΠΈΠΊΠ°: string SourcePath = @"D:\SourceDir\"; string TargetPath = @"D:\TargetPath\"; //ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Ρ„Π°ΠΉΠ»Ρ‹ ΠΈ ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ ΠΈΡ…: IEnumerable<string> files = nGetFiles(SourcePath, LookForExt); foreach (f in files) //МоТно Π±Ρ‹Π»ΠΎ ΠΈ Ρ‚Π°ΠΊ: foreach (f in nGetFiles(@"D:\SourceDir", LookForExt)) { try { File.Copy(f, TargetPath + Path.GetFileName(f), true); } catch (Exception e) { throw e; } } } 

As a result, all files in the SourcePath folder and its subfolders will be scanned, the found files corresponding to the required extensions will be copied to the TargetPath without saving the subfolders structure. Moreover, if there is a file D:\SourceDir\test.asp and there is a file D:\SourceDir\111\test.asp , then in the resulting folder D:\TargetPath\ will be only one of these files that will be copied last ( and overwrite the existing one, and the first file copied to the target folder).

If you need to copy files from a folder while preserving the structure of subfolders and files, then here already gave the answer .

  • I tried your method, does not copy all files, only .txt formats, why not all files are copied? - GooliveR
  • @ArteS, and what files in the source folder were? The file filter is set here: string LookForExt = "\.jpg|\.txt|\.asp"; - BlackWitcher
  • Yes, it was there that added extensions to files, but it turned out this way, only the "\.txt" format is copied, and a couple of "\.jpg" format files. Why is this happening? - GooliveR
  • one
    Strange, but try replacing foreach (f in files) with foreach (f in nGetFiles(@"D:\SourceDir", LookForExt)) . I did not have such a problem, that’s the thing, the code is used in a real program. - BlackWitcher
  • If you throw only * .jpg in the amount of 10 pieces in the source directory, will they be copied? - BlackWitcher

Create an array with extensions, loop through it and look for files with the desired extension, then loop through all the found files of one extension and copy one to the correct directory, in my case copy from D:\files to D:\files\copied

 public static void SearchAllFiles() { string path = @"D:\files\"; // ΠΎΡ‚ΠΊΡƒΠ΄Π° string pathTo = @"D:\files\copied\"; // ΠΊΡƒΠ΄Π° string[] extensions = { "*.jpg", "*.txt", "*.asp" }; foreach (string ext in extensions) { foreach (string file in Directory.GetFiles(path, ext, SearchOption.AllDirectories)) { File.Copy(path + Path.GetFileName(file), pathTo + Path.GetFileName(file)); } } } 
  • one
    You can also remove the loop by extensions , replacing the file search with Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) .Where(s => extensions.Any(e => s.EndsWith(e)) - Vlad
  • Thank you for the examples, I will delve into, just wanted to learn on .Net 2.0 above the way to speed up is possible? - GooliveR
  • @ArteS, yes. Instead of EnumerateFiles, call GetFiles. - Vlad
  • @Vlad, And the last question: Is it possible to make a crawl to a specific folder, for example, I need to prevent passing the Users folder, since there are also many folders and files in it, how to make a crawl? - GooliveR
  • @ArteS, I do not know how to do this using the GetFiles method. Alternatively, you can add another sample condition to the example above: .Where(_ => !_.Contains(Path.Combine(path, "Users"))) - Vlad