1

I'm trying to write code that checks whether the Acrobat xx.x folder is present on a user's PC, where xx.x represents a potential Acrobat version number, such as Acrobat 10.0 or Acrobat 11.1. Meaning I do not know what xx.x will be. The answer found here seems to assume I know what value the wildcard takes, but I don't in my case.

The purpose is to eventually write .js files into the directory ...\Program Files\Adobe\Acrobat xx.x\Acrobat\Javascripts

So far I identify whether 32/64-bit OS is installed and set the Program Files path into progFiles. Assuming that Adobe is installed, I then need to use this path in order to determine whether Acrobat xx.x is a subfolder such as:

folderToFind = new File(progFiles + "\\Adobe\\"+"\\Acrobat 11.1");

where 11.1 may take the form of any number from 0.1 to 99.9. I then can identify its existence with:

if (folderToFind.exists())
  {
    System.out.println("Acrobat folder found");
  }

An obvious approach is to create a loop, checking whether each and every possibility exists. This seems redundant and unnecessary. I'm hoping there's a more efficient approach such as:

  if (progFiles + "\\Adobe\\"+"\\Acrobat **.*\\Acrobat\\Javascripts".exists()){
 // ...
     }

Any ideas? Thank you

Community
  • 1
  • 1
Mathomatic
  • 899
  • 1
  • 13
  • 38
  • [This SO post might answer](http://stackoverflow.com/questions/8752034/how-to-check-a-file-if-exists-with-wildcard-in-java) – gtgaxiola Dec 10 '15 at 19:44
  • I've read that already and it seems to assume that I know what `XXX` will be... but in my case I'm looking for a dynamic number from 0.1 to 99.9. – Mathomatic Dec 10 '15 at 19:46

1 Answers1

1

You use a FileFilter

And look for Files that match your description: Acrobat \d{1,2}\.\d

Example:

    File dir = new File(path);
    File[] matchingFiles = dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            String regex = "Acrobat \\d{1,2}\\.\\d";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(pathname.getName());
            return m.matches();
        }
    });
    for(File f : matchingFiles) {
        System.out.println(f.getName());
    }

You have complete control on how to build your Pattern

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64