Hi @Sushil Agarwal , Welcome to Microsoft Q&A,
The Regex.IsMatch method is used to match whether the file name matches the pattern of the regular expression ^{expectedFilename}(\d+)$, which means that the file name starts with expectedFilename, followed by a number in parentheses, and then the file extension. . If a file name matches this pattern, it is marked as a duplicate file.
I test smaller quantities and am able to complete them quickly. If you are searching for 9495 you may need to wait.
private void button1_Click(object sender, EventArgs e)
{
string folderPath = @"C:\Users\Administrator\Desktop\test";//path_to_your_folder
string[] filePrefixes = { "10x23", "45x23" }; // Add other prefixes
int numBills = 25;//9495
foreach (var filePrefix in filePrefixes)
{
var (missingFiles, duplicateFiles) = FindMissingOrDuplicateFiles(folderPath, filePrefix, numBills);
Console.WriteLine($"Missing files for prefix {filePrefix}:");
foreach (var missingFile in missingFiles)
{
Console.WriteLine(missingFile);
}
Console.WriteLine($"\nDuplicate files for prefix {filePrefix}:");
foreach (var duplicateFile in duplicateFiles)
{
Console.WriteLine(duplicateFile);
}
}
}
static (List<string>, List<string>) FindMissingOrDuplicateFiles(string folderPath, string filePrefix, int numBills)
{
var filesFound = new HashSet<string>();
var missingFiles = new List<string>();
var duplicateFiles = new List<string>();
for (int i = 1; i <= numBills; i++)
{
string expectedFilename = $"{i}x{filePrefix}";
string standardFilename = $"{expectedFilename}.pdf";
if (File.Exists(Path.Combine(folderPath, standardFilename)))
{
filesFound.Add(standardFilename);
}
else
{
missingFiles.Add(standardFilename);
}
foreach (var file in Directory.GetFiles(folderPath))
{
var filename = Path.GetFileNameWithoutExtension(file);
if (Regex.IsMatch(filename, $@"^{expectedFilename}\(\d+\)$"))
{
duplicateFiles.Add(filename + ".pdf");
}
}
}
return (missingFiles, duplicateFiles);
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.