How to search Folder in Obsidian

Sometimes, I just want to search for folders (not notes) by keywords and quickly navigate to them. So I wrote this script:

Demo:
10d7110c-1a2f-4c2f-8526-873b5eb7de13

Method: Install the Templater plugin, then use this Templater script:

<%*
// Get all folder paths
let folders = app.vault.getAllFolders();
// Filter out the root directory
folders = folders.filter(folder => folder.path !== "/");

const autoCollapse = true;

// Prepare folder names and corresponding paths for display
const folderPaths = folders.map(f => f.path);
const folderNames = folders.map(f => {
    // Get folder depth for indentation display
    const depth = f.path.split('/').length - 1;
    const indent = '  '.repeat(depth);
    // Use πŸ“ emoji to represent folders
    return `${indent}πŸ“ ${f.name} (${f.path})`;
});

// Use Templater's suggester to create a selector
// The first parameter of tp.system.suggester is the array or function of displayed text, the second is the array of actual values
let selectedFolderPath = await tp.system.suggester(folderNames, folderPaths);

// Exit if the user cancels the selection
if (selectedFolderPath === null) return;

// Get the selected folder object
const selectedFolder = app.vault.getAbstractFileByPath(selectedFolderPath);

// If the folder is found, focus on it in the File Explorer
if (selectedFolder) {
    if (autoCollapse) {
        app.workspace.getLeavesOfType('file-explorer')[0].view.tree.setCollapseAll(true)
    }
    
    try {
        // Try to focus on the folder using internal API
        const fileExplorerLeaves = app.workspace.getLeavesOfType('file-explorer');
        // Check if there are available leaf nodes
        if (fileExplorerLeaves.length === 0) {
            // If not, create a new file explorer tab (refer to source code logic)
            const leaf = app.workspace.getLeaf('tab');
            await leaf.setViewState({ type: 'file-explorer' });
            fileExplorerLeaves.push(leaf);
        }
        // Get the first file explorer view instance
        const fileExplorerView = fileExplorerLeaves[0].view;

        // Force activate the file explorer tab (ensure the view is visible)
        app.workspace.revealLeaf(fileExplorerLeaves[0]);

        const targetFile = app.vault.getAbstractFileByPath(selectedFolderPath);
        fileExplorerView.revealInFolder(targetFile);
    } catch (error) {
        // If all methods fail, show a notice
        new Notice( `Unable to focus on folder in file explorer: ${selectedFolderPath}, please search manually` );
    }

    // Show a notice indicating the operation was successful
    new Notice(`Selected folder: ${selectedFolderPath}`);
} else {
    new Notice(`Folder not found: ${selectedFolderPath}`);
}
%>

You can also register a shortcut key for it to make it more convenient to use.

Script download: TP Script: Search Folder

(English version is on the bottom)

1 Like

I also developed a plugin for navigating to folders. It has already been submitted to the Obsidian community plugin store for review.

1 Like

Still can’t see the plugin in the community yet, but it looks good :+1:t2:

Wait I’ve seen your β€œζ•ˆηŽ‡δΈζˆη˜Ύβ€ articles LOL
I have an impression of the AI-assisted development plugin section mentioned in it – and this plugin – too!

2 Likes

I received some feedback from the developer. Hopefully, it will be available soon in the community plugin store.

I installed it using GitHub - TfTHacker/obsidian42-brat: BRAT - Beta Reviewer's Auto-update Tool for Obsidian. and it works great