右键添加获取可供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 文件静默启动终端。

相关推荐
jz_ddk12 分钟前
[LVGL] 从0开始,学LVGL:进阶应用与项目实战(上)
linux·信息可视化·嵌入式·gui·lvgl·界面设计
Bruce_Liuxiaowei21 分钟前
Windows系统错误6118全面解决方案:修复此工作组的服务器列表当前无法使用
运维·服务器·windows·网络安全
望获linux36 分钟前
【实时Linux实战系列】Linux 内核的实时组调度(Real-Time Group Scheduling)
java·linux·服务器·前端·数据库·人工智能·深度学习
MC丶科1 小时前
【SpringBoot常见报错与解决方案】端口被占用?Spring Boot 修改端口号的 3 种方法,第 3 种 90% 的人不知道!
java·linux·spring boot
江公望1 小时前
ubuntu kylin(优麒麟)和标准ubuntu的区别浅谈
linux·服务器·ubuntu·kylin
Lynnxiaowen1 小时前
今天我们开始学习python语句和模块
linux·运维·开发语言·python·学习
生态笔记1 小时前
PPT宏代码
linux·服务器·powerpoint
mucheni2 小时前
迅为RK3588开发板Ubuntu 系统开发ubuntu终端密码登录
linux·运维·ubuntu
skywoodsky2 小时前
Ubuntu 24.04环境下的挂起转休眠
linux
水饺编程2 小时前
第3章,[标签 Win32] :窗口类03,窗口过程函数字段
c语言·c++·windows·visual studio