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

相关推荐
cosX+sinY32 分钟前
ubuntu 20.04 复现 LVI-SAM
linux·ubuntu·机器人·bash
冬瓜31232 分钟前
物联网外设管理服务平台
linux·运维·驱动开发
wd52052135 分钟前
常用环境部署(二十六)——Centos搭建MQTT服务端EMQX
linux·运维·centos
程序员JerrySUN1 小时前
驱动开发硬核特训 · Day 1
java·linux·运维·开发语言·c++·驱动开发
愚润求学1 小时前
【Linux】Git的简单使用
linux·服务器·git
搜搜秀2 小时前
Linux(CentOS10) gcc编译
linux·运维·服务器·c语言
jzy37113 小时前
实战:动态网络中的路由管理──优化连接的策略与实践之双网卡打通内外网
windows
孤独得猿3 小时前
[linux复习]——多线程
java·linux·jvm
小灿同学啊4 小时前
vmware虚拟机上Ubuntu或者其他系统无法联网的解决方法
linux·ubuntu
假如我年华正好4 小时前
【教程】MacBook 安装 VSCode 并连接远程服务器
linux·服务器·ide·vscode·ssh·macbook