(Node.js子进程)带有/select选项的生成资源管理器不适用于路径中的空格



我正在编写一个Node脚本,该脚本执行;在资源管理器中显示文件";作用我的代码可以归结为:

const {spawn} = require('child_process');
// This works (it opens C:/path/to/file and highlights the file named "WithoutSpaces"
spawn('explorer', ["C:\path\to\file\WithoutSpaces,", "/select"]);
// However, this does not work. It defaults to opening a file explorer window with my Documents folder.
spawn('explorer', ["C:\this path\has spaces,", "/select"]
spawn('explorer', ["C:\this path\has spaces\file.txt,", "/select"]
// Strangely, spaces DO work if I'm not doing the /select option:
spawn('explorer', ["C:\this path\has spaces,", "/select"]

奇怪的是,当我使用/select参数来选择文件时,它只在路径不包含任何空格时才起作用。如果路径确实有空格,则默认为Documents文件夹。

有办法解决这个问题吗?也许是用反斜杠对空间进行编码的一种方法?或者explorer命令的其他一些特殊参数?

您缺少特定于窗口的windowsVerbatimArguments: true选项,该选项在execspawn的文档中都列出,但很容易错过。至关重要的是,默认情况下,它设置为false,这与您想要的相反。

以下操作很好:

const path = require("path");
const {exec, spawn} = require("child_process");
spawn(`explorer`, [
`/select,"${path.join(`C:`, `Program Files (x86)`, `Common Files`)}"`
], { windowsVerbatimArguments: true });

最新更新