How to increase the speed of checking files for existence for network resources&

47 Views Asked by At

I have tried various options. At first it was File.Exists(). But it runs for 2 seconds on each element. And I have, for example, 60 elements. It takes 2 minutes.

Then I tried getting a list of paths and getting a list of all files under a common path via Directory.GetFiles(). But this option gives an error. The user name or password is incorrect. : '\server\FileStorage\files'

Dictionary<int, string> files; // { 1, "files\\folder/5.3.231/file.exe" }, { 2, "files\\folder/5.1.1/file.exe"}, { 3, "files\\folder/4.1/sub/file55.exe" }

foreach (var key in files.Keys)
{
    var path = Path.Combine("\\\\server\\FileStorage\\", files[key]);
    files[key] = Path.GetFullPath(path);
}

HashSet<string> extensions = new();
var values = files.Values.ToList();
values.ForEach(path => extensions.Add(Path.GetExtension(path)));

var folder = new string(values.First().Substring(0, values.Min(s => s.Length))
    .TakeWhile((c, i) => values.All(s => s[i] == c))
    .ToArray()); // "\\\\server\\FileStorage\\files\\folder/"

var filePathsExists = new List<string>();

foreach (var extension in extensions)
{
    filePathsExists.AddRange(Directory.GetFiles(folder, $"*{extension}", SearchOption.AllDirectories));
}

for (var i = 0; i < filePathsExists.Count; i++)
{
    filePathsExists[i] = Path.GetFullPath(filePathsExists[i]);
}

var result = files.Where(x => filePathsExists.Contains(x.Value))
    .ToDictionary(x => x.Key, x => x.Value);

Any ideas

1

There are 1 best solutions below

0
Guru Stron On

Not a full answer but there is at least one problem in your code - your resulting common folder is determined incorreclty. You might want at least substring after last \:

var lastIndexOfAny = folder.LastIndexOfAny(new[] { '\\', '/' });
if (lastIndexOfAny >= 0)
{
    folder = folder.Substring(0, lastIndexOfAny + 1);
}

Notes

  • if your path is too short (i.e. you will need to enumerate too many folders) you might want to go with by file approach still
  • no need to use ToList on Dictionary.Values (unless you are going to modify it in the process - but that is whole other can of worms) - it is already should be a materialized collection.