Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

Below is the code written on BUTTON_CLICK. After clicking on button files from SOURCE should copy to DESTINATION.

In SOURCE, files will be placed in multiple folders as shown below:

C:\\users\A\B\010\100 Files
C:\\users\A\B\011\100 Files
C:\\users\A\B\012\100 Files

But in DESTINATION, i need files to copy as per below folder structure as shown below:

D:\\users\Y\Z\001\25 Files
D:\\users\Y\Z\002\25 Files
D:\\users\Y\Z\003\25 Files

As per above example, it should create a sub-folder named 001 inside the destination path and folder limit should be only 25 files. After copying 25 files, it should copy the rest 25 into 002 folder.

I managed the code till filtering the specific file extensions in SOURCE path and copying to DESTINATION path.

Can you please help me folder threshold?

Below is the code written in button click:

private void button2_Click(object sender, EventArgs e)
        {   
            string source = textBox1.Text;
            string destination = textBox2.Text;
            var thresholdvalue = textBox3.Text;
            var extensions = new[] { ".tif", ".tiff", ".jpg", ".jpeg" };
 var files = Directory.GetFiles(source, "*.*", SearchOption.AllDirectories);//.
            //Where(S => extensions.Contains(Path.GetExtension(S).TrimStart('.').ToLowerInvariant()));
            foreach (var item in files)
            {
                if(extensions.Contains(Path.GetExtension(item)))
                {
                    File.Copy(item, destination + Path.GetFileName(item));
                }
            }
            var sizes = Directory.GetFiles(destination, "*.*", SearchOption.AllDirectories).Count();
            MessageBox.Show("All " + sizes + " files are copied");
}


What I have tried:

i have tried adding sub-folders but failed to create threshold value dynamically.
Posted
Updated 7-Sep-20 23:02pm

1 solution

Try something like this:
C#
if (!int.TryParse(textBox3.Text, out int thresholdValue) || thresholdValue < 1)
{
    // TODO: Display an error message to the user
    return;
}

string source = textBox1.Text;
string destination = textBox2.Text;

int totalFileCount = 0;
int currentSubFolder = 0;
int remainingFileCount = 0;
string destinationFolder = null;

ISet<string> extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) 
{
    ".tif", 
    ".tiff", 
    ".jpg", 
    ".jpeg" 
};

IEnumerable<string> files = Directory
    .EnumerateFiles(source, "*", SearchOption.AllDirectories)
    .Where(f => extensions.Contains(Path.GetExtension(f)));

foreach (string sourceFile in files)
{
    if (remainingFileCount == 0)
    {
        // First file, or the sub-folder is full:
        currentSubFolder++;
        destinationFolder = Path.Combine(destination, currentSubFolder.ToString("D3"));
        if (!Directory.Exists(destinationFolder)) Directory.CreateDirectory(destinationFolder);
        remainingFileCount = thresholdValue;
    }
    
    string destinationFile = Path.Combine(destinationFolder, Path.GetFileName(sourceFile));
    File.Copy(sourceFile, destinationFile);
    totalFileCount++;
    remainingFileCount--;
}

MessageBox.Show("All " + totalFileCount + " files are copied");
Now do yourself a favour and give your controls meaningful names, rather than accepting the default names provided by Visual Studio. Sure, you might remember what textBox42 represents now, but when you come back to your code in a couple of weeks, you'll have forgotten.
 
Share this answer
 
Comments
Member 8010354 8-Sep-20 9:50am    
Thank you sir for your help on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900