How can i find missing bills pdf saved in a folder

Sushil Agarwal 381 Reputation points
2024-02-14T12:35:52.8766667+00:00

Hello experts can you guide how can i find missing or duplicate files in a folder where files are stored as BillNumberxBillSerisxFinYear.pdf 1x10x23.pdf ... 9495x10x23.pdf [ it may have duplicate files 10x10x23(1).pdf, 10x10x23(2).pdf 1x45x23.pdf ... 3000x45x23.pdf

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,846 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,346 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 34,506 Reputation points Microsoft Vendor
    2024-02-15T03:35:49.55+00:00

    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.