Vscode: Get selected file/folder in the explorer view

Created on 29 Feb 2016  路  22Comments  路  Source: microsoft/vscode

It would be nice if it was possible to retrieve the selected file/folder in the explorer view. The use case where I want to use it for is my vscode-yo extension to be able to run the generator in the selected directory. An API for this would be nice.

api feature-request file-explorer

Most helpful comment

I see that support for multi-file select is there in explorer view. In my extension i want to get the paths of all selected files. Right now, even if I select 5 files, i only get path for 1. I am not finding any relevant API. Do we have the API available for the same?

All 22 comments

@jrieken fyi

This would be helpful for my vscode-hexdump extension, to retrieve selected file not compatible with TextDocument (i.e. png, jpg, ...). I wouldn't need to show an inputbox to ask for the file path everything :)

Second that. It will help us for the extension we are developing @Sencha. Here is the link to Stack Overflow question on the same feature.

Some extension need retrieve selected file/folder, such as TortoiseSVN.
If can get selected file/folder, even if not integrate SVN, also can be very convenient use SVN by contextmenu of explorer.

Maybe the menus contribution we have added this summer helps anyone here. The contract is that when invoked from a resource (such as an explorer entry) its URI will be passed to the function

When a command is invoked from a (context) menu, VS Code tries to infer the currently selected resource and passes that as a parameter when invoking the command. For instance, a menu item inside the Explorer is passed the URI of the selected resource and a menu item inside an editor is passed the URI of the document.

@jrieken executing a command from the menu, I get the uri as expected, except it doesn't work when that command is invoked from a KB shortcut (I get an empty parameter instead).

except it doesn't work when that command is invoked from a KB shortcut

Yeah, thats the design of things as KB shortcuts are global (not contextual) by definition... It's like someone using you command-id with the executeCommand-api. The rule to always be prepared to be invoked without sufficient arguments. In those cases you need to figure them out by yourself. In this case by using an API that you don't have (yet)

with some api or not?

I find out a way to get the path of selected folder. You can contribute a menu of explorer,then run the vscode.commands.executeCommand('copyFilePath');. You can get the value into your clipboard

In this case by using an API that you don't have (yet)

@jrieken Do you have an idea for API? Something like window.activeResource, that returns a current selected resource of an active part?

no

I too need this function for a plugin. Has forecast for implementation?

In Theia was implemented.

I need this feature now. Has it ever been implemented or is there another way to get the selected items in the explorer view?

I need to search for specific files but only in the currently selected folder.

I was surprised today this feature is not existing.

Would be very convenient to have a way to get a list of selected files / folders as well.

This feature is still not there.

am not sure if this solve ur issue, but maybe this could help

  • show on folder only
"menus": {
    "explorer/context": [
        {
            "when": "explorerResourceIsFolder",
            "command": "extension.hello_world",
            // ...
        }
    ]
}
  • show on files only
"menus": {
    "explorer/context": [
        {
            "when": "!explorerResourceIsFolder",
            "command": "extension.hello_world",
            // ...
        }
    ]
}

and for the command

vscode.commands.registerCommand('extension.hello_world', (e) => {
    let path = e.fsPath

    // do ur work
})

@ctf0 It does not. Extension code is activated by either from the command pallette or a shortcut. There is no interaction with the context menu in the explorer view.

Instead I try to find the currently active editor and trace back form that file.

Just to add my own use case here--I'm looking to enable users to select multiple files within the explorer, which my extension can then act on. As of right now, this doesn't appear to be possible without replicating the entire workspace folder structure in a custom TreeView (not even 100% sure it's possible then). I'd much prefer to just be able to retrieve the current list of selected files/folders on the OOTB explorer view.

I would like to add my usecase as explained here: https://stackoverflow.com/questions/59909657/open-default-file-when-folder-is-selected-in-vscode

I believe we have a good variety of usecases now to back up this request!

I see that support for multi-file select is there in explorer view. In my extension i want to get the paths of all selected files. Right now, even if I select 5 files, i only get path for 1. I am not finding any relevant API. Do we have the API available for the same?

To add to the party, I thought I'd add a related, but different use case. I am not and do not want to build an extension. I am trying to build a _Debug_ task when working with Rust. Rust will build a separate executable per integration test file. The debugging experience works just fine, but changing the launch.json file all the time to choose a different startup item doesn't make sense. The simplest bridge would seem to be to choose the currently selected file, but there is no such variable for this. I can make it work with the ${file} variable, but only after opening the actual executable file and clicking through the _Do you really want to open this file_ prompt. That's workable, but very clunky.

Having an API for extension authors, including Rust, would be great, but it seems there is a use case for just having a variable that exposes that information is also useful. An API would certainly be more comprehensive for multi-select and so. Two new variables that simply indicate the currently selected folder and file would seem to be congruent with the existing ${selectedText} variable.

+1 for wanting this!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lw-schick picture lw-schick  路  229Comments

ozsay picture ozsay  路  247Comments

jez9999 picture jez9999  路  234Comments

Perkovec picture Perkovec  路  191Comments

mariusa picture mariusa  路  219Comments