在WSL中以编程方式运行时,explorer.exe无法打开正确的文件夹

Somekindofnick

我正在尝试explorer.exe从在WSL Ubuntu 20.04内运行的Node.js脚本打开我遇到的问题是,explorer.exe永远不要打开我想要的文件夹。代替WSL用户的主目录,而是打开Windows用户的Documents文件夹。我应该怎么做才能explorer.exe打开我想要的文件夹?

这是我尝试过的:

该脚本首先定义了一个execShellCommandpromisify函数exec然后,自执行函数首先使用转换process.env.HOME为Windows路径wslpath然后explorer.exe,将转换后的路径作为参数执行。

#!/usr/bin/node

const execShellCommand = async cmd => {
  const exec = require('child_process').exec
  return new Promise((resolve, reject) => {
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        console.warn(error)
      }
      resolve(stderr ? stderr : stdout)
    })
  })
}

;(async () => {
  const path = await execShellCommand(`wslpath -w "${process.env.HOME}"`)
  console.log({ path })
  await execShellCommand(`explorer.exe ${path}`)
})()

在WSL中运行脚本时获得的输出

$ ./script.js 
{ path: '\\\\wsl$\\Ubuntu-20.04\\home\\user\n' }
Error: Command failed: explorer.exe \\wsl$\Ubuntu-20.04\home\user


    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:315:20)
    at maybeClose (internal/child_process.js:1048:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) {
  killed: false,
  code: 1,
  signal: null,
  cmd: 'explorer.exe \\\\wsl$\\Ubuntu-20.04\\home\\user\n'
}

explorer.exe不管输出中显示的错误如何运行。奇怪的是,如果我运行相同的命令,我的脚本试图explorer.exe \\\\wsl$\\Ubuntu-20.04\\home\\user\n直接在WSL终端explorer.exe运行(),则确实会打开我想要的文件夹。在路径末尾修剪新行没有帮助。

马克

我认为您必须对产生的反斜杠进行其他转义wslpath以下代码对我有用,这意味着它将在Windows资源管理器中打开正确的目录

注意:它仍然会抛出您提到的错误,我认为这是由于节点退出的方式而不是由于执行explorer.exe; 我绝对不是节点专家。

#!/usr/bin/node

const execShellCommand = async cmd => {
          const exec = require('child_process').exec
          return new Promise((resolve, reject) => {
                      exec(cmd, (error, stdout, stderr) => {
                                    if (error) {
                                                    console.warn(error)
                                                  }
                                    resolve(stderr ? stderr : stdout)
                                  })
                    })
}

;(async () => {
          let path = await execShellCommand(`wslpath -w "${process.env.HOME}"`)
          console.log("before", {path});
          path = path.replace(/\\/g,"\\\\");
          console.log("after", {path});
          await execShellCommand(`explorer.exe ${path}`)

})()

甚至比替换反斜杠还干净,我认为这可以通过将$HOME变量直接解析到命令行中来为您工作:

await execShellCommand(`explorer.exe "$(wslpath -w $HOME)"`);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用上层文件夹中的vbs脚本打开exe文件

关闭通过Explorer.exe打开的文件夹

如何在运行时从程序包中以编程方式配置karaf / etc文件?

从文件夹中读取文件,尽管索引正确,但我收到“文件错误(文件,“rt”):无法打开连接”错误

Total Commander无法在OneDrive文件夹中打开文件

如何在文件夹名称中使用逗号打开 explorer.exe 中的文件夹?

使用gradle以编程方式添加资源文件夹以测试编译/运行时类路径

无法从C linux中的/ proc位置打开文件夹

无法在 git 中打开最近推送的文件夹

无法在本地主机中打开项目文件夹

LinuxBash无法在当前文件夹中打开

使用TMUX时无法在Sublime中打开文件夹

mkdir在../中创建文件夹-无法打开流

无法在Linux nerdtree vim中打开文件夹

无法在Visual Basic中打开图片文件夹

Python 程序在从文件夹或桌面打开时无法正常运行,但在 IDLE 中运行时运行良好

如何使用 explorer.exe (Cygwin) 在 Ranger 中打开文件夹

我无法在 wp 正确的文件夹目录中访问

以管理员身份运行时无法访问当前目录中的文件夹

Excel vba 使用相同的 explorer.exe 实例打开文件夹

当路径中的文件夹中有空格时,无法直接从文件夹中打开PowerShell文件

我无法在运行时以编程方式设置android:below

使用Spring Boot在运行时以编程方式更改application.properties文件

在指定文件夹中以编程方式运行npm install

如果文件以编程方式打开,则 Excel 工作簿 ADO 查询自身在首次运行时失败

即使选择了“在一个窗口中打开文件夹”,Explorer.exe也会打开新进程

在Windows上将应用程序运行时数据存储在哪个正确的文件夹中?

在jupyter中打开时无法查看文件夹中的ipynb文件

资产文件夹中的Pdf文件无法在第三方应用中打开