右键添加获取可供WSL使用的路径,对windows文件夹也适用,即获取符合Linux规范的路径内容给WSL

文章目录

  • [1. 功能展示](#1. 功能展示)
    • [1.1. 对 WSL 文件/文件夹/目录空白位置 使用](#1.1. 对 WSL 文件/文件夹/目录空白位置 使用)
    • [1.2. 对 Windows 文件/文件夹/目录空白位置 使用](#1.2. 对 Windows 文件/文件夹/目录空白位置 使用)
    • [1.3. Fin](#1.3. Fin)
  • [2. 方法](#2. 方法)
  • [3. 文件内容](#3. 文件内容)
    • [3.1. AddWSLPath.reg](#3.1. AddWSLPath.reg)
    • [3.2. CopyPath.vbs](#3.2. CopyPath.vbs)
  • [4. 念念碎](#4. 念念碎)

1. 功能展示

1.1. 对 WSL 文件/文件夹/目录空白位置 使用

输出 /etc

1.2. 对 Windows 文件/文件夹/目录空白位置 使用

输出 /mnt/d/hty/creat/code

1.3. Fin

获得的文件/文件夹路径可直接在 WSL 终端内直接使用:

2. 方法

新建内容如后所述的 CopyPath.vbsAddWSLPath.reg 文件,可用记事本创建,然后修改后缀名。

需要修改 AddWSLPath.regC:\\myScripts\\CopyPath.vbs 为你的 CopyPath.vbs 的路径,假设你的 CopyPath.vbs 放在 D:\AAA, 则写入:

复制代码
[HKEY_CLASSES_ROOT\Directory\shell\Path for WSL\command]
@="wscript.exe \"D:\\AAA\" \"%1\""

AddWSLPath.reg 的 3 个路径位置都要修改。然后令 AddWSLPath.reg 用注册表编辑器打开,使之加入注册表即可。

如果功能没生效,打开注册表编辑器检查是否写入了。如果没写入,说明路径填错了:

  • 比如结尾少了个 \
  • 或者路径中的 \ 没有用 \\ 进行转义。

3. 文件内容

3.1. AddWSLPath.reg

复制代码
Windows Registry Editor Version 5.00

; by 萌豚老师

[HKEY_CLASSES_ROOT\Directory\shell\Path for WSL]
@="Path for WSL"

[HKEY_CLASSES_ROOT\Directory\shell\Path for WSL\command]
@="wscript.exe \"C:\\myScripts\\CopyPath.vbs\" \"%1\""

[HKEY_CLASSES_ROOT\*\shell\Path for WSL]
@="Path for WSL"

[HKEY_CLASSES_ROOT\*\shell\Path for WSL\command]
@="wscript.exe \"C:\\myScripts\\CopyPath.vbs\" \"%1\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\Path for WSL]
@="Path for WSL"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Path for WSL\command]
@="wscript.exe \"C:\\myScripts\\CopyPath.vbs\" \"%V\""

3.2. CopyPath.vbs

复制代码
' by 萌豚老师

Set objArgs = WScript.Arguments
If objArgs.Count = 0 Then
    WScript.Echo "No argument provided"
    WScript.Quit
End If

' 获取第一个参数
strPath = objArgs(0)

' 使用 PowerShell 将路径复制到剪贴板
Set objShell = CreateObject("WScript.Shell")

Dim resultPath
resultPath = ""

Dim slashCount, i
slashCount = 0

' 处理 WSL 路径
If Left(strPath, 2) = "\\" Then
    ' 从第5个 \ 开始截取路径
    For i = 1 To Len(strPath)
        If Mid(strPath, i, 1) = "\" Then
            slashCount = slashCount + 1
        End If
        
        If slashCount >= 4 Then
            resultPath = Mid(strPath, i ) ' 从第5个斜杠后开始保留路径
            Exit For
        End If
    Next
Else
    ' 处理 Windows 路径
    resultPath = "/mnt/" & LCase(Left(strPath, 1)) & Mid(strPath, 3) 
End If

' 把路径中的 \ 替换为 /
resultPath = Replace(resultPath, "\", "/")

' 使用 PowerShell 将处理后的路径复制到剪贴板
On Error Resume Next ' Continue on error to handle possible clipboard issue
objShell.Run "powershell -Command Set-Clipboard '" & resultPath & "'", 0, True

4. 念念碎

  • 为什么不在注册表中直接用 cmd/Powershell 实现相关功能?

因为即使使用了 '-WindowStyle Hidden' 等功能,仍然会短暂显示一个终端窗口,在 https://github.com/PowerShell/PowerShell/issues/3028 也有提出。目前该问题仍未解决,所以比较好的方法是用 vbs 文件静默启动终端。

相关推荐
tan180°8 分钟前
Boost搜索引擎 查找并去重(3)
linux·c++·后端·搜索引擎
yongui4783424 分钟前
CentOS系统如何查看当前内存容量
linux·运维·centos
xzq_java30 分钟前
CentOS操作系统虚拟机安装以及连接工具下载和远程连接工具远程连接
linux·运维·centos
子兮曰1 小时前
🔥C盘告急!WSL磁盘暴增?三招秒清20GB+空间
前端·windows·docker
逐梦吧!旅行者2 小时前
Linux之环境变量(内容由浅入深,层层递进)
linux·运维
IOT-Power2 小时前
树莓派 Ubuntu 24.04 开机换源总结
linux·数据库·ubuntu
weixin_468466852 小时前
树莓派32位与64位系统安装teamviewer远程软件
linux·单片机·自动化·树莓派·远程控制·vnc·teamviewer
挨踢攻城2 小时前
Linux 应急响应实操 Checklist
linux·运维·linux命令·rhce·rhca·厦门微思网络·linux 应急响应
optiz2 小时前
细菌基因组genome二代测序数据分析
linux·运维·服务器
学历真的很重要3 小时前
Claude Code Windows 原生版安装指南
人工智能·windows·后端·语言模型·面试·go