How to remove FileInfo in Array List that already processed in .Take()

here is the code :

        // Files has 25 PDF
        var Files = Folder.GetFileToPublicFolder(Folder.srcFolder);

        foreach (FileInfo file in Files)
        {
            // Get 10 PDF in Files
            List<FileInfo> files = Files.Take(10).ToList();

            // Process the 10 PDF
            foreach (var item in files)
            {                    
                File.Move(Path.Combine(Folder.srcFolder, item.Name), Path.Combine(Folder.tmpFolder, item.Name));                    
            }

            files = null;

            ProcessParallelThread(e);
        }      

I have Public Folder that has 25 PDF Files.

Using this

List<FileInfo> files = Files.Take(10).ToList();

it will get the 1 - 10 PDF and processed it. After processing the 1 - 10 PDF when foreach loops again it take the same 1 - 10 PDF not the 11 - 20 PDF.

How can i get the Other PDF in List<>?

Thank you in Advance!

Answers


Take does as the name suggest just take 10 elements (the first 10 to be more specific) there is no way that the 2nd take call does take different 10 elements if you don't tell it.

If you want to process chunks of 10 Elements i would suggest creating a method that does the splitting (Separation of Concerns)

This is what i use:

public static class EnumerableExtensions
{
    [Pure]
    public static IEnumerable<T[]> Split<T>(this IEnumerable<T> source, int chunkSize)
    {
        T[] sourceArray = source as T[] ?? source.ToArray();
        for (int i = 0; i < sourceArray.Length; i += chunkSize)
        {
            T[] chunk = new T[chunkSize + i > sourceArray.Length ? sourceArray.Length - i : chunkSize];
            Array.Copy(sourceArray, i, chunk, 0, chunk.Length);
            yield return chunk;
        }
    }
}

now you got a split method which can be used like this

var files = Folder.GetFileToPublicFolder(Folder.srcFolder);
foreach(var chunk in files.Split(10))
{
   //....
}

After your first loop, you have to call:

files = Files.Skip(10).Take(10).ToList();

I am using your code and made some changes to make it working for more than 20 files if you need.

  // Files has 25 PDF
        var Files = Folder.GetFileToPublicFolder(Folder.srcFolder);

        int _skip = 0;

        foreach (FileInfo file in Files)
        {
            // Get 10 PDF in Files
            List<FileInfo> files = Files.Skip(_skip).Take(10).ToList();

            // Process the 10 PDF
            foreach (var item in files)
            {
                File.Move(Path.Combine(Folder.srcFolder, item.Name), Path.Combine(Folder.tmpFolder, item.Name));
            }

            files = null;

            ProcessParallelThread(e);
            _skip = _skip + 10;
        }

Hope it will work for you.


Need Your Help

Javascript code not working in chrome

javascript google-chrome jquery

I'm Using this JavaScript code with JQuery Library, Now this doesn't work in Google Chrome, works in Apple Safari and FireFox (As tested)

genisoimage script cannot burn bigger than 4GB but less than 4.5GB file?

bash shell sh iso

I found it strange after running the following script file.