简体   繁体   中英

Opening Explorer to a specific file in Node.js

I've looked at some related questions, and there exist ways of opening explorer.exe to a specific file, such that it is selected in the Explorer window. The command line goes something like this:

explorer.exe /select,"C:\\Temp\\Myfile.png"

I ran that command in the command prompt directly to verify that it works, and it does. However, I cannot get it to run in Node in a good way. Some things I've tried:

const expl = exec('cmd.exe', ["explorer.exe", `/select,"${root}\\${filename}\"`]);
const expl = spawn('cmd.exe', ["explorer.exe", `/select,"${root}\\${filename}\"`]);
const expl = exec('cmd.exe', [`explorer.exe /select,"${root}\\${filename}\"`]);

...and some other variations. I didn't / don't really know what I am doing.

I ended up writing a truly ugly solution:

function openExplorerSelected(filename){

  let batfile = `explorer.exe /select,\"${root}\\${filename}\"`;
  fs.writeFile("tmp.bat", batfile, function(err){
    if( err ) console.warn(err);
    else {
      const expl = spawn('cmd.exe', ["/c", "tmp.bat"]);

    }
  })
}

It works, but ew.

What is the right way of doing this?

您是否尝试将explorer.exe作为可执行文件而不是cmd.exe运行?

const expl = exec('explorer.exe', [`/select,"${root}\\${filename}\"`]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM