Jump to content

Find folders with exact name only


Recommended Posts

I would like to search for folders by their exact name, and exclude other folders starting with or otherwise containing the search phrase.

 

Say I have folders:

Object

Object 1

Object 12

 

And I have files:

Object

Object 1

Object 12

 

I want to search for "Object" and return all folder(s) with the exact name "Object", regardless of their paths/parent folders. I don't want any files returned and I don't want folders "Object 1" "Object 12".

 

I can currently get all folders containing "Object" using a File Filter limited to File Type public.folder with Words and Split unchecked in the Field tab. Or I can check those boxes and the result will be limited to folders starting with "Object" but will also pull in "Object 1" "Object 12" etc.

 

I thought there would be an obvious way to limit the results in the Value of the Field tab, but I'm not finding a way to make that work.

 

Thanks

Link to comment
  • 2 years later...

Would still like to search for folder by its exact name, and not get other files or folders with similar names or names containing (but not exactly matching) the search phrase. Any thoughts on how to accomplish this?

Link to comment

Not thoroughly tested and not the cleanest way to do it, but this should work:

 

readonly escaped="$(sed "s|'|\\\'|g;s|/|:|g" <<< "${1}")"
readonly results="$(/usr/bin/mdfind -literal "kMDItemFSName == '${escaped}' && kMDItemContentType == public.folder")"

osascript -l JavaScript -e 'function run(argv) {
  items = argv[0].split("\n").map(arg => {
    return {
      title: $(arg).lastPathComponent.js,
      subtitle: arg,
      arg: arg,
      icon: { type: "fileicon", path: arg }
    }
  })

  return JSON.stringify({ items: items })
}' "${results}"

 

Stick it in a Script Filter with default settings.

Link to comment

Thanks, @vitor. This is perfect.

 

I did some googling and modified to make the search case-insensitive by added a "c" after the "escaped" variable like so:

readonly results="$(/usr/bin/mdfind -literal "kMDItemFSName == '${escaped}'c && kMDItemContentType == public.folder")"

 

Works great!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...