C # How to rename MANY files faster or at the same time?

2
So, I'm creating a program here to rename files, the code I've made is very slow and only renames one file at a time, and can not create a code to rename all at once because they are distinct numbers and not is in sequence which might make it easier.

Code;

        DirectoryInfo pasta = new DirectoryInfo(folderPath);

        FileInfo[] adxs1 = pasta.GetFiles();
        foreach (FileInfo names in adxs1)
        { File.Move(names.FullName, names.FullName.ToString().Replace("old_00079", "new_00098")); }

        FileInfo[] adxs2 = pasta.GetFiles();
        foreach (FileInfo names in adxs2)
        { File.Move(names.FullName, names.FullName.ToString().Replace("old_00091", "new_00105")); }

So, the sequence is this, and there are MANY files over 1000 rsrs. What I wanted to know about you is if I can, for example, rename all or one part at a time, even if it's different numbers?

    
asked by anonymous 23.05.2017 / 01:52

1 answer

3

You can use parallelism:

var movesSimultaneos = 2;
var moves = new List<Task>();

foreach (var filePath in Directory.EnumerateFiles(folderPath))
{
    var move = new Task(() =>
    {
        File.Move(filePath, filePath.ToString().Replace("old_00079", "new_00098"));
    }, TaskCreationOptions.PreferFairness);
    move.Start();

    moves.Add(move);

    if (moves.Count >= movesSimultaneos)
    {
        Task.WaitAll(moves.ToArray());
        moves.Clear();
    }
}

Task.WaitAll(moves.ToArray());

I translated from here .

Addendum

File.MoveTo() performs better .

    
23.05.2017 / 02:06