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

相关推荐
林开落L8 分钟前
库制作与原理(下)
linux·开发语言·centos·库制作与原理
wxy31920 分钟前
嵌入式LINUX——————TCP并发服务器
java·linux·网络
Castamere28 分钟前
配置 Linux 终端 (zsh)
linux
LZQqqqqo2 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
季春二九2 小时前
Windows 11 首次开机引导(OOBE 阶段)跳过登录微软账户,创建本地账户
windows·microsoft
小韩博2 小时前
metasploit 框架安装更新遇到无法下载问题如何解决
linux·网络安全·公钥·下载失败
长臂人猿2 小时前
JVM常用工具:jstat、jmap、jstack
linux·运维·jvm
轻松Ai享生活2 小时前
揭秘 linux:一张图看懂系统配置的核心
linux
wdxylb3 小时前
云原生俱乐部-RH134知识点总结(2)
linux·云原生
芥子沫3 小时前
Jenkins常见问题及解决方法
windows·https·jenkins