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

相关推荐
Caron_xcb32 分钟前
大数据——解决Matplotlib 字体不足问题(Linux\mac\windows)
大数据·linux·matplotlib
水水沝淼㵘1 小时前
嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
linux·服务器·c语言·数据结构·学习·算法·排序算法
愚润求学1 小时前
【Linux】基础 IO(一)
linux·运维·服务器·开发语言·c++·笔记
越甲八千2 小时前
windowsC++操作ADB
c++·windows·adb
大白的编程日记.2 小时前
【Linux学习笔记】理解一切皆文件实现原理和文件缓冲区
linux·笔记·学习
孞㐑¥2 小时前
Linux之进程控制
linux·开发语言·c++·经验分享·笔记
愚润求学2 小时前
【Linux】简单设计libc库
linux·运维·开发语言·c++·笔记
chennalC#c.h.JA Ptho2 小时前
Bodhi linux 系统详解
linux·经验分享·笔记·系统架构·系统安全
九班长3 小时前
Mirror的多人连接管理及房间系统
windows
一个懒鬼3 小时前
Edge浏览器打开PDF文件显示空白(每次需要等上一会)
windows·pdf