autohotkey:复制文件名而不包含路径和扩展名

时间:2018-04-22 13:53:37

标签: loops parameters path copy autohotkey

欢迎大家

以下代码在这里:https://autohotkey.com/boards/viewtopic.php?f=5&t=44848

撰稿:Odlanir

        q::
    fileselected := Explorer_GetSelection()
    SplitPath,fileselected, fn
    MsgBox % "FileName :`t" fn "`nFullName :`t" fileselected
    clipboard = % "FileName :`t" fn "`nFullName :`t" fileselected
    return


    Explorer_GetSelection(hwnd="") {
        WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
        WinGetClass class, ahk_id %hwnd%
        if (process = "explorer.exe")
            if (class ~= "Progman|WorkerW") {
                ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%
                Loop, Parse, files, `n, `r
                ToReturn .= A_Desktop "\" A_LoopField "`n"
        } else if (class ~= "(Cabinet|Explore)WClass") {
            for window in ComObjCreate("Shell.Application").Windows
                if (window.hwnd==hwnd)
                    sel := window.Document.SelectedItems
            for item in sel
                ToReturn .= item.path "`n"
        }
    return Trim(ToReturn,"`n")


}
return

当您单击热键时,我们会获取所选文件的文件名和路径, 随着图片附上

https://drive.google.com/open?id=1VvPKYrbVQEfrmdB34dUND9HbnXMl0Lr2

我的问题是:如何只按文件名获取列表,没有文件路径和文件扩展名 随着图片附上

https://drive.google.com/open?id=1rCuLikjqT8Nudipby5Y9MOHTqdozGPYn

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

新编辑的代码:

q::
fileselected := Explorer_GetSelection()
MsgBox % fileselected
clipboard = % fileselected
return


Explorer_GetSelection(hwnd="") {
    WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
    WinGetClass class, ahk_id %hwnd%
    if (process = "explorer.exe")
        if (class ~= "Progman|WorkerW") {
            ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%
            Loop, Parse, files, `n, `r
            ToReturn .= A_Desktop "\" A_LoopField "`n"
    } else if (class ~= "(Cabinet|Explore)WClass") {
        for window in ComObjCreate("Shell.Application").Windows
            if (window.hwnd==hwnd)
                sel := window.Document.SelectedItems
        for item in sel
            ToReturn .= item.path "`n"
    }
 fileselected:=ToReturn . "`n"
 while RegExMatch(fileselected,"s)\\(.*)")
 {
 RegExMatch(fileselected,"s)(.*?)(\n)",filestemp)
 RegExMatch(filestemp,"(.*)\.",filestemp)
  while RegExMatch(filestemp1,"s)\\(.*)")
   RegExMatch(filestemp1,"s)\\(.*)",filestemp)
 filesfin := filesfin . filestemp1 . "`n"
 RegExMatch(fileselected,"\n(.*)",filestemp)
 fileselected:=filestemp1
 }
 return (filesfin)


}
return

答案 1 :(得分:0)

更简单的解决方案可能只是编辑一行:

SplitPath fileselected, fNameExt, fDir, fExt, fName, fDrv

然后以任何您想要的方式使用fName或任何其他结果。

当然,您可以省略其他参数:

SplitPath fileselected,,,, fName