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

I developed a windows application where it should copy the files from INPUT location to OUTPUT location in a proper Folder structure

EXAMPLE: 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, files are copying as per below folder structure as shown below which is good and required in the same way only:

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


But, if user opens another instance or they tried to copy another set of data in the same OUTPUT folder (which has already 003 folders), it should start with 004 folder by checking the previous folders but it is again copying as 001 folder only

How can i make the code work to check the folder number and increment.

COPY CODE BUTTON CLICK:

  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");


What I have tried:

i tried using the condition but it is not at all working
Posted
Comments
BillWoodruff 22-Oct-20 0:01am    
are you saying you want to process each one of 3 directories each one of which has 100 files ? how do you select 25 of the 100 files ?

we can't read your mind :)
Member 8010354 22-Oct-20 0:46am    
No what i'm saying is when i tried to copy the files from source location to destination location, it should copy in a proper folder structure (as shown below)

001 - > 1000 files (number of files to be copied is dynamic by providing the user defined value which is already coded)
002 - > 1000 files
003 - > 1000 files

TILL HERE IT IS WORKING

if i try to copy the files to again same location (same files or same source path or anything), it is again creating the folder from 001 instead it should check the folder number already existed and increment it
as per our above example, it should create 004 folder and start copying instead of 001 again.
Richard MacCutchan 22-Oct-20 4:40am    
You first need to check what folders already exist at the lowest level of the destination. So if it is folders "001", "002", "003", then you need to set the folder id to the last value plus 1.
[no name] 22-Oct-20 6:58am    
I see no reason why you don't just use the same names as the "source" (based on what you've said so far). Considering all the "confusion", maybe just do a "rename" when you're done doing what you think you're doing.

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