0

I have a python class that I want to use with an imageJ macro. The python class(below) will walk(search) a directory and look for multiple files given by the user. I need to use this logic with an ImageJ macro to walk a directory and return a list with references to the files I need. Is there a way I can use this class with an imageJ macro or somehow recreate this within the imageJ macro?

import sys, os
from fnmatch import fnmatch

def searchDirectory():
    root = "\home"
    fileNames = ["test.txt", "plshelp.cpp"]
    resultPaths = []

    for path, subdirs, files in os.walk(root):
      for name in files:
        for target in fileNames:
          if fnmatch(name, target):
            if checkMatches(name, resultPaths):
              resultPaths.append(os.path.join(name))

    return resultPaths


def checkMatches(name, targets):
    flag = True

    for target in targets:
      if fnmatch(name, target):
        flag = False

    return flag
  • Please explain step by step what you want to obtain. Analyzing your code is tedious. As far as I understand, an ImageJ-macro will be able to search directories and check for file names or parts of names. – Herbie May 23 '23 at 15:20
  • I would like the code to traverse a directory for a given file name and return a list of all the file paths with that file name. We will be analyzing images and they are in an umbrella directory, with multiple files sharing the same name. – anonymousKoala May 23 '23 at 15:26
  • Files having the same name in the same directory? How is this possible? – Herbie May 23 '23 at 15:30
  • Different sub-directories, its like an umbrella – anonymousKoala May 23 '23 at 15:32
  • So you need to search through all subdirectories of a given directory. This is easily possible with an ImageJ-macro. Please start ImageJ and go to "Help>>Examples>>Macro>>Process Folder". This code inspects all subfolders. In function "processImage" you need not open the files but only check their names. Good luck! – Herbie May 23 '23 at 15:40

0 Answers0