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)
{
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)
{
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