Skip to content Skip to sidebar Skip to footer

How To Get Folder Path Using Electron

I am very new to the electron. Can anyone suggest me how to get a local folder's relative path using the electron? JavaScript does not have that capability. I have a Choose File b

Solution 1:

As @phuongle pointed out in the comments you want to use showOpenDialog(). Something like this:

var remote = require('remote');
var dialog = remote.require('electron').dialog;

var path = dialog.showOpenDialog({
    properties: ['openDirectory']
});

UPDATE: If the above isn't working for your current Electron version, you should try more modern importing:

const {dialog} = require('electron').remote;

In addition, in order to use remote, you need to set enableRemoteModule when creating your window in your main process:

const myWindow = newBrowserWindow({
    webPreferences: {
        enableRemoteModule: true
    }
});

Solution 2:

In Electron we can select the directory by specifying simple input element with type="file" and webkitdirectory attribute'. <input id="myFile" type="file" webkitdirectory /> and we can get the directory full path with the path property of File object document.getElementById("myFile").files[0].path

Solution 3:

You would use Node's path.relative for that.

Solution 4:

The solution for me was simply using all in lowercase, with a value true as string in my react component. No extra configuration was required.

Like so:

<inputid="path-picker"type="file"
    webkitdirectory="true"
/>

Edit

It turns out that, as mentioned by @cbartondock, it will recursively look for files in the directory, which is not good!

I ended up using the required electron remote's dialog.

Post a Comment for "How To Get Folder Path Using Electron"