簡體   English   中英

(Node.js 子進程)生成資源管理器,帶有 /select 選項,不適用於路徑中的空格

[英](Node.js child process) Spawning explorer, with /select option, does not work with spaces in path

我正在編寫一個執行“在資源管理器中顯示文件”功能的 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命令的其他一些特殊參數?

您缺少特定於 Windows 的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 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM