0

I have a folder of files with names like D0000025667-T04292.pdf, D0000025668-T02119.pdf, D0000025670-T01125.pdf and so on.

I have an MS access database table, with values as follows:

**Dealer Code        Email**
T04292            a@gmail.com
T04292            a@gmail.com
T02119            b@gmail.com
T01125            b@gmail.com
RS0009            c@gmail.com
RS0001            c@gmail.com
C01020            d@gmail.com

I need to get the 'For Dealer' code from the table and search in folder. Finally I need to attach matching files to an email to send to the address listed in the database e.g. Dealer Code T04292 matches file D0000025667-T04292.pdf which needs to be sent to a@gmail.com.

I have one mail function where I can directly specify the attaching file path, but now I need to search and match the files need to attach.

Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
User9999
  • 95
  • 1
  • 3
  • 9

1 Answers1

2

I am answering one single question, combined questions are not useful (besides, attaching files to a mail is already explained in other answers, e.g. here). So here is how to find matching files:

class DealerFilter implements FilenameFilter
{
    private final String dealer;

    DealerFilter(String dealer)
    {
        this.dealer = dealer;
    }

    @Override
    public boolean accept(File dir, String name)
    {
        // uncomment these lines for debugging:
        //System.out.println ("dealer: " + dealer);
        //System.out.println ("name:   " + name);
        //System.out.println ("dir:    " + dir);

        return name.toLowerCase().endsWith("-" + dealer.toLowerCase() + ".pdf");
    }
}

// returns null if error, empty array if not found
File[] findDealerPDFs(String directory, String dealer)
{
    File dir = new File(directory);

    File[] files = dir.listFiles(new DealerFilter(dealer));
    if (files == null)
    {
        // error
        return null;
    }
    for (File f : files)
    {
        System.out.println(f);
    }
    return files;
}
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • That is the full code. Just call findDealerPDFs("directory", "dealer"); from your program. And uncomment the two lines that are commented. Post what you get in a comment or in chat. – Tilman Hausherr Aug 21 '15 at 11:37
  • Then you didn't pass any dealer values to the findDealerPDFs() call. You need to make a call like this: findDealerPDFs("/blah/dibla/directory", "T04292"); – Tilman Hausherr Aug 21 '15 at 11:52
  • now im doing lik this I am getting Code from DB and passing it to findDealerPDFs() call.. – User9999 Aug 21 '15 at 11:59