Processing Decisions for Relevant Files (and Folders)
Distinguishing Files in Folders:

In the array section we have seen that we can cycle through a given folder and print file paths. Folders, however, might contain multiple different file types as well as sub-folders and we might need to process only some of them. Therefore, we need to distinguish between files and folders and potentially different file types.

We can check for example if a file name ends on a specific file extension, begins with a certain sequence of letters or if a file in a file list is actually a folder.

This we do with:
  • startsWith("text to be checked for specific beginning", "beginning");
  • endsWith("text to be checked for specific ending", "ending");
Apart from that, we can check if a certain text (such as a file or image name) contains a specific sequence of letters at any location in the text.
  • matches("text", "search term");
Advantage of matches(); is that it accepts Regular Expressions (RegEx) in the search term. This means that we can more flexibly search for things.

Regular Expression examples:


  • . (a dot) = any character (letters, numbers, special characters)
  • * = any amount of ...
  • [A-Z|a-z] = all single upper case and lower case letters
  • \d = any digit
  • ...
Therefore, to check if a certain text contains the word "Analysis" or "analysis" besides other characters or numbers using RegEx looks like:

.*[A|a]nalysis.* 


Why do we want to exclude any "Analysis" folder from the processing?

This is something you will see in the following sections. If we are not careful with folder handling, we can quickly run into endless loop problems with our macro.

There are two reasons why we should identify our output folders and prevent them from being further processed:

  1. Our macro creates the results folders with the name "Analysis" automatically and those do not need to be re-analyzed, since they only contain the output of the macro and no input images.
  2. If we would process the "Analysis" folders as well, our macro would create a new "Analysis" sub-folder inside each Analysis folder and therefore create an endless loop of many hundred "Analysis" sub-sub-sub-sub-...folders inside each other.

This means that the macro would enter in an unstoppable "downwards" spiral in creating hundreds or thousands of nested "Analysis" folders in each other. This will only stop if it crashes or you restart the computer.

The resulting file path of those nested folders is so long that some computer systems do not allow you to easily delete it. Therefore, try to avoid that!

This is were the matches(); command comes very handy to exclude those from further consideration. If we would create the endless loop folder creation it might even be difficult to delete such a sub-folder tree from your computer due to a too long file path. So, be very careful to not enter into this rabbit hole.