Jump to content

Search folder on NAS starting from a predefined folders list


Recommended Posts

Hi,

 

I need a workflow that searches folders located on NAS starting from a list of predefined folders. I need also to limit searches to 1 folder depth on NAS for speed and accurate search result.

 

Example:

 

I want to tell Alfred to look only inside this set of folders:

 

/volumes/data/folder1

/volumes/data/folder2

/volumes/data/folder3

 

and limit the folder depth to 1 so only folders inside the folder list are searched.

 

Using the 'find' command it would be:

 

find /volumes/data/folder1 -maxdepth 1

find /volumes/data/folder2 -maxdepth 1

find /volumes/data/folder3 -maxdepth 1

 

Note:

NAS is not indexed.

 

Link to comment

If it isn't indexed, you'll have to write your own workflow that displays the results of those find calls.

A very basic example (using my Alfred-Workflow library) would look like this:
 

from __future__ import print_function, unicode_literals

import sys
import hashlib
import subprocess
import argparse

from workflow import Workflow, ICON_WARNING

CACHE_RESULTS_FOR = 10  # seconds

log = None
decode = None


def find(dirpath, query):
    cmd = ['find', dirpath, '-maxdepth', '1', '-name', '*{}*'.format(query)]
    output = decode(subprocess.check_output(cmd))
    return [l.strip() for l in output.split('\n') if l.strip()]


def main(wf):
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--folder',
                        help="Search in this folder")
    parser.add_argument('query', help="Search for files with this name")
    args = parser.parse_args(wf.args)

    dirpath = args.folder
    query = args.query

    def wrapper():
        return find(dirpath, query)

    key = '{}::{}'.format(dirpath, query).encode('utf-8')
    m = hashlib.md5()
    m.update(key)
    key = m.hexdigest()

    results = wf.cached_data(key, wrapper, max_age=CACHE_RESULTS_FOR)

    if not results:
        wf.add_item('No results found', valid=False, icon=ICON_WARNING)

    for path in results:
        wf.add_item(path, valid=True, arg=path, icon=path, icontype='fileicon')

    wf.send_feedback()


if __name__ == '__main__':
    wf = Workflow()
    log = wf.logger
    decode = wf.decode
    sys.exit(wf.run(main))

Here's a full workflow to get you started. I've created one Script Filter that searches ~/Downloads. You can change that path or duplicate the Script Filter to add other directories.

Edited by deanishe
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...