我正在尋找一種方法,使 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 回饋中心應用程式」上提出回饋請求。原生支援將是首選,但我認為微軟不會在任何合理的時間範圍內這樣做。所以我們只能透過這種 hacking 自己來做。正斜線是更好的跨平台路徑分隔符號。永遠不要讓微軟支配你任何事。享受 ;-)
答案3
在 PowerShell 中執行以下函數:
function slash {
Get-Clipboard |
% {$_ -replace '\\','/'} |
Set-Clipboard ; echo 'Conversion done.'
}
然後你可以透過複製路徑並slash
在powershell中運行來使用它。
這不是直接執行此操作的方法,但如果您只需要它進行小型連續會話並且不想安裝任何第三方應用程序,那麼它就可以工作。
答案4
這可以透過使用一些可以存取剪貼簿的腳本來實現,它的工作方式如下:
- 選擇並複製包含 的路徑
\
。 - 按一些熱鍵來啟動腳本。
- 此腳本存取剪貼簿的內容並使用簡單的邏輯來替換每次出現的
\
to/
。 - 現在剪貼簿內容包含帶有
/
.
腳本可能是批次文件或一個自動熱鍵腳本。但我不認為蝙蝠可以存取剪貼簿內容。所以 Autohotkey 會是最好的選擇。