我正在寻找一种方法,让 Windows 始终将路径显示为斜杠而不是反斜杠,以便在将路径复制粘贴到 Java 代码时省去很多麻烦。
为了更清楚一点,我想要这样的默认呈现方式:
是这样的:
我该怎么做?任何帮助我都会非常感激,因为这快让我抓狂了。
答案1
答案2
我按照 Ankit 的建议实现了它。实现过程有点棘手,因为重写的路径不能再用于粘贴实际文件(而不是文本路径)。我通过检测我猜测只会使用文本表示的情况来解决这个问题。从技术上讲,据我了解 Windows 剪贴板实现,甚至可以只更改路径的文本表示,但我把这个任务留给了别人。不确定是否可以使用 AutoHotKey 来完成。
因此,AutoHotKey 脚本是按照以下工作流程编写的:
- 按照通常的方式复制路径。
- 我们尽力确保剪贴板中的路径干净。
- 如果我们不能默认执行此操作(我们不知道它是否保存),我们什么也不做,您必须通过按Shift+ Super+手动清理剪贴板中的路径C。
详细信息请看代码:
#SingleInstance force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, RegEx
;; Use this until I hit the first issue then document here and set back to default value.
SetDefaultMouseSpeed, 0
;; Copy clean file/directory path to clipboard (use forward slashes as file separators) {{{
;; https://stackoverflow.com/questions/1589930/so-what-is-the-right-direction-of-the-paths-slash-or-under-windows/1589959#1589959
;; WARNING: This clipboard substitution has the issue that after the substitution, pasting the file does not work anymore!!
;; Because of this, we don’t run the substitution OnClipboardChange globally but only when we consider it save and otherwise using a (manual) shortcut.
;; Situations where we know it is save:
;; * Double Commander calls CopyFullNamesToClip.
;; * Location bar in Explorer has focus. See limitations below!
;; The expected workflow is:
;; 1. Do what you usually do to copy a path.
;; 2. We try to do what is necessary to have a clean path in the clipboard.
;; 3. If we cannot do it by default (we don’t know that it is save), we do nothing and you have to manually make the path in the clipboard clean by pressing Shift+Super+C.
;; Ref: Get-CleanPath in ../../MS_Shell/Modules/ypidDotfiles/ypidDotfiles.psm1
;; Seems up to and including Windows 10, UNC paths with forward slashes don’t work.
;; At least //files.example.org/home and \\files.example.org/home and //files.example.org\home don’t work.
clean_path_in_clipboard() {
If (RegExMatch(Clipboard, "^(?i)(?:[a-z]:)?\\[^\\]")) {
StringReplace, Clipboard, Clipboard,\,/, All
}
Return
}
;; Shift+Super+C | Clean file/directory path in clipboard {{{
+#C::
; ClipSaved := ClipboardAll
; Clipboard =
; Send ^c
;; Ensure that we are only working on text.
; ClipWait
; currentPath =
; WinGetClass explorerClass, A
; ControlGetText currentPath, Edit1, ahk_class %explorerClass%
; msgbox %currentPath%
; If (ErrorLevel) {
; Clipboard := ClipSaved
; MsgBox, 48, Clipboard copy warning, Failed to copy to clipboard.
; Return
; }
clean_path_in_clipboard()
Return
;; }}}
;; Shift+Alt+C | Hook Double Commander calls to CopyFullNamesToClip and run clean_path_in_clipboard afterwards.
;; We can "safely" do this because when CopyFullNamesToClip is called, the user wants to copy the path as text.
#UseHook
#IfWinActive ahk_exe doublecmd.exe
+!c::
Send +!c
clean_path_in_clipboard()
Return
#IfWinActive
#UseHook off
OnClipboardChange:
;; Fix file path when in transit in Explorer (or Double Commander).
;; Ensure that we are only working on text.
If (WinActive("ahk_exe (?i)(?:explorer.exe|doublecmd.exe)") and A_EventInfo == 1) {
;; Location bar in Explorer has focus.
;; Run clean_path_in_clipboard after copying text to clipboard in Explorer when cursor is above "Location bar" known as Edit1 (bad programming/variable naming M$??).
;; Technically this is not 100 % bulletproof because you could do the copy to clipboard with Ctrl+L followed Ctrl+C while the cursor focuses some other control.
If (WinActive("ahk_exe (?i)(?:explorer.exe)")) {
MouseGetPos, , , , control_below_cursor
If (control_below_cursor == "Edit1") {
clean_path_in_clipboard()
}
}
;; We cannot do this globally, see WARNING above.
; clean_path_in_clipboard()
}
return
;; }}}
(GitHub 上也有此记录:https://github.com/ypid/dotfiles/blob/master/windows/neo-vars/source/custom.ahk)
还要感谢 Katharsas 在“适用于 Windows 10 的反馈中心应用程序”中打开反馈请求。最好有原生支持,但我认为微软不会在合理的时间内做到这一点。所以我们只能通过这种黑客攻击自己做。正斜杠是更好的跨平台路径分隔符。永远不要让微软指挥你任何事情。尽情享受 ;-)
答案3
在 PowerShell 中运行以下函数:
function slash {
Get-Clipboard |
% {$_ -replace '\\','/'} |
Set-Clipboard ; echo 'Conversion done.'
}
然后您可以通过复制路径并slash
在 powershell 中运行来使用它。
这不是直接的方法,但如果您只需要进行小型连续会话并且不想安装任何第三方应用程序,它就可以完成工作。
答案4
这可以通过使用一些可以访问剪贴板的脚本来实现,它将以这种方式工作:
- 选择并复制包含的路径
\
。 - 按下某些热键来激活脚本。
- 该脚本访问剪贴板的内容并使用简单的逻辑来替换每个出现
\
的/
。 - 现在剪贴板内容包含带有 的路径
/
。
脚本可能是批文件或自动热键脚本。但我不认为蝙蝠可以访问剪贴板内容。所以 Autohotkey 是最好的选择。