Electron open file explorer with file selected43 sec read

After creating a file and writing it to the disk, its nice to show the user the location of the newly created file in the system file explorer.

At the time of writing this there is no option in electron for opening the system file explorer with a file selected. So, like every programmer does I put the title of this post into the google search bar and found this

https://discuss.atom.io/t/opening-the-oss-file-explorer/24348

So this is what I did

function open_file_exp(fpath) {
  var command = '';
  switch (process.platform) {
    case 'darwin':
      command = 'open -R ' + fpath;
      break;
    case 'win32':
      if (process.env.SystemRoot) {
        command = path.join(process.env.SystemRoot, 'explorer.exe');
      } else {
        command = 'explorer.exe';
      }
      command += ' /select,' + fpath;
      break;
    default:
      fpath = path.dirname(fpath)
      command = 'xdg-open ' + fpath;
  }
  console.log(command);
  child_process.exec(command, function(stdout) {
    //Do something if you really need to
  });
}

 

Leave a comment

Your email address will not be published. Required fields are marked *