Hi, I want to read source directory and copy each file to the destination by skipping after each file based on interval value i.e., if i set the value of interval 2, then it should skip two files everytime.

For Example: If there are 5 files in a directory, 1.txt, 2.txt, 3.txt, 4.txt and 5.txt and i have set the interval = 1, then my program should skip one text file after each file and copy 1, 3 and 5.txt to the destination.
I have a code which simply copies from source to destination.

static void Main(string[] args)
        {
            string fileName;
            string destFile;
            string sourcePath = @"E:\Source";
            string targetPath = @"E:\Destination";
            // To copy all the files in one directory to another directory.
            string[] files = System.IO.Directory.GetFiles(sourcePath);
            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }

Recommended Answers

All 8 Replies

    static void Main(string[] args)
    {
        string fileName;
        string destFile;
        string sourcePath = @"E:\Source";
        string targetPath = @"E:\Destination";
        // To copy all the files in one directory to another directory.
        string[] files = System.IO.Directory.GetFiles(sourcePath);
        // Copy the files and overwrite destination files if they already exist.
        int skipNum = 1; //as per the example, set this however you like.
        int countNum = 0; //used to track how many files copied since last skipped file.
        foreach (string s in files)
        {
            if (countNum != skipNum)
            {
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
                countNum++;
            }
            else
            {
                countNum = 0;
            }
        }
    }

Have written this in the reply without testing but its pretty simple so should work hopefully, lemme know any errors and i'll resolve.

Thanks Mike....

If we change the value of skipNum to 2 then it first copies 2 files into the directory and skip 1.
But I want to read (copy) first and then skip 2 and then read (copy) 4th and then skip 2....

Ah shoot sorry missed that when reading the reqs, gimme a min, going to put this into visual studio and fix it.

Code is tweaked and tested. With a skipNum of 2, I got files copied: 1, 2, 5, 6, 9, 10 of 10 files.

static void Main(string[] args)
        {
            string fileName;
            string destFile;
            string sourcePath = @"C:\Users\maskew\Documents\DaniWeb\Source";
            string targetPath = @"C:\Users\maskew\Documents\DaniWeb\Destination";
            // To copy all the files in one directory to another directory.
            string[] files = System.IO.Directory.GetFiles(sourcePath);
            // Copy the files and overwrite destination files if they already exist.
            bool skipNextFiles = false;
            int skipNum = 2; //as per the example, set this however you like.
            int countNum = 0; //used to track how many files copied since last skipped file.
            foreach (string s in files)
            {
                if (skipNextFiles)
                {
                    if (countNum != skipNum)
                    {
                        countNum++;
                        if (countNum == skipNum)
                        {
                            countNum = 0;
                            skipNextFiles = false;
                        }
                    }
                }
                else
                {
                    if (countNum != skipNum)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        fileName = System.IO.Path.GetFileName(s);
                        destFile = System.IO.Path.Combine(targetPath, fileName);
                        System.IO.File.Copy(s, destFile, true);
                        countNum++;
                        if (countNum == skipNum)
                        {
                            countNum = 0;
                            skipNextFiles = true;
                        }
                    }
                }
            }
        }

It is working fine but still copying each time 2 consecutive files, but i want to copy 1 each time and then skip 2.

Sorry, easy fix, replace the current bottom if statement with:

                    if (countNum != skipNum)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        fileName = System.IO.Path.GetFileName(s);
                        destFile = System.IO.Path.Combine(targetPath, fileName);
                        System.IO.File.Copy(s, destFile, true);
                        skipNextFiles = true;
                    }

Thanks MIKE....Its working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.