AHK code to locate the residing folder and highlight the active file

╄→尐↘猪︶ㄣ 提交于 2019-12-23 06:00:26

问题


It’s often requires to quick locate the folder location of open active file and highlight or select the active file while working on different software applications. Quick locating the residing folder needed for finding related files in same folder, rename of the opened files or residing folder or move the file to different related folder. Current options require navigating through the loads of folders to find and locate the specific folder where it’s buried with bunch of similar other files (similar to find needle in a haystack). Microsoft Office suite has built-in feature named “document location” which can be added to quick access toolbar. But it only allow to see the folder location or full path but no single click command or key available (AFAIK) to conveniently jump to that locating folder and highlight/identified the opened file so that further operation (e.g. rename, move) could be done on that specific file/folder. This is also the case for other software applications where native program have options to get full path but no way to jump to the specific file/folder. Considering one of Microsoft Office suites application (e.g. word) as test cases the processes I could imagine as follows;

  • 1 Get the full path (D:\Folder\Subfolder\Mywordfile.docx) of currently opened word document
  • 2 Close the file
  • 3 Explorer command to select and highlight the file in folder given full path (process 1)
  • Operation on file/folder as desire manually and double click to return to file operating applications (e.g. word).

In my assessment for Implementation of above tasks following are possibilities

  • Task 1 Microsoft Word has a built-in function called "document location" to get the full path of the opened document and its currently possible to copy the file path in the clipboard.
  • Task 2 Close the file (Ctrl+W or Ctrl+F4)
  • Task 3 AHK code for Explorer command to select the file for a given full path (available in Task 1)

I am facing difficulties in Task 3 and I tried each of these but so far no luck

Clipboard := “fullpath” ; Full path (D:\Folder\Subfolder\Mywordfile.docx ) copied from Word 

Run explorer /e, “Clipboard” 

Run %COMSPEC% /c explorer.exe /select`, "%clipboard%"

So far above explorer command only take me to my documents folder not in the specific folder location (path in Task 1). I am curious know what would be the right explorer code to select the file for a given full path in clipboard. Appreciate for supporting AHK code or better way to do this task. Thank in advance.


回答1:


I'm not clear on why your sample code doesn't work. I suspect it's because of the extra characters.

After running this command Windows Explorer will be open and have the desired file selected (if it exists).

FullPathFilename := "e:\temp\test.csv"
Explorer := "explorer /select," . FullPathFilename
Run, %Explorer%



回答2:


I don't know if you tried the other approach, but I think this is simpler and shorter:

  • 1) Store the full path of the document in a string: oldfile = ActiveDocument.FullName

  • 2) SaveAs the document with ActiveDocument.SaveAs

  • 3) Delete the old file with Kill oldfile

All this is from VBA directly, no need to use Explorer shell. The same exists for the other applications.

Here is a fully working code for the Word Documents:

Sub RenameActiveDoc()

    Dim oldfile As String

    Set myDoc = ActiveDocument

    '1) store current file
    oldfile = myDoc.FullName

    '2) save as the active document (prompt user for file name)
    myDoc.SaveAs FileName:=InputBox("Enter new name", "Rename current document", myDoc.Name)

    '3) Delete the old file with
    On Error GoTo FileLocked
    Kill oldfile
    On Error GoTo 0

    Exit Sub

FileLocked:
    MsgBox "Could not delete " & oldfile, vbInformation + vbOKOnly, "File is locked"

End Sub



回答3:


With contribution of Ro Yo Mi I am able to come up with following solution. However I am assuming that there might better solution to this task.

 ;;; Customize Document Location (Choose form All Commands) in Quick Access Toolbar and get its position (#4 for my case)
#If WinActive("ahk_class OpusApp") || WinActive("ahk_class XLMAIN") || WinActive("PPTFrameClass")
#p:: ;Close Word/Excel/PowerPoint Document and Locate in Explorer Folder Location
clipboard = ;empty the clipboard
Send !4 ; Select full path while document location at #4 position in Quick Access toolbar
Send ^c ; copy the full path
ClipWait ; waits for the clipboard to have content
Send {esc}
Send, ^{f4} ; Close opened document only but keep Word/Excel/PPT program running
Explorer := "explorer /select," . clipboard
Run, %Explorer%\
return


来源:https://stackoverflow.com/questions/37102222/ahk-code-to-locate-the-residing-folder-and-highlight-the-active-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!