I'm currently using the following snippet to go through millions of files in a very large directory and then copy the ones that I need into another working directory. sNos
is an int[]
which holds some integers. I check if the filename contains one of these integers, if Yes, it is copied into my local directory.
string[] allFiles = System.IO.Directory.GetFiles(@"C:\ExampleFolder");
foreach (string file in allFiles)
{
for (int i = 0; i < sNos.Count(); i++)
{
if (file.Contains(sNos[i].ToString()))
{
File.Copy(file, "C:\\newFolder\\" + file.Substring(file.Length - 25), true);
}
}
}
Now, to be specific.. the filenames are in the format of XXXXXX_XX_XX_XX_XXX_XX
. X
denoting an integer. The first 6 numbers in the filename is what I try to match with values in my int array. The problem is this, there can be files with names like:
123456_33_42_56_234_44 (Size: 1 MB)
123456_33_46_34_992_23 (Size: 2 MB)
Now, since both files will match "123456" in my int array, both will be copied. However, I only want the larger file to be copied everytime there is a match with multiple files. There can be a match with 2 files, maybe 3 or even more. How can I go about doing this? Any help will be appreciated!