How to retrieve the full adress of a selected file form the Windows File Manager

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

09 Nov 2021, 08:28

Code: Select all

If !WinExist("ahk_class FreeCommanderXE.SingleInst.1 ahk_exe FreeCommander.exe")
 Return
SoundBeep, 1500
WinActivate
Send ^1
WinMinimize
Return
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

09 Nov 2021, 23:40

Thank you very much for your last post.

I continue to try to find a solution to make a copy of backup of the files/folders that I will select directly on my desktop.

I find on this site the possibility to recuperate the name of files/folders which are on the desktop:


Code: Select all

shellWindows := ComObjCreate("Shell.Application").Windows
window := shellWindows.Item(ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 8))
shellFolderView := window.Document
for item in shellFolderView.Folder.Items
   items .= (!items ? "" : "`n") . item.Path
MsgBox, % items

This code work good, but it exist also other possibilities on the page:

viewtopic.php?style=17&t=60403#p255256.

Now I need to mix this code with your code:

Code: Select all

If !FileExist(dest := "M:\Backup") {
 FileCreateDir, %dest%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  ExitApp
 }
}
tmstmp := SubStr(A_YYYY, 3) A_MM A_DD "-" A_Hour A_Min
For each, file in A_Args {
 target := dest "\" tmstmp " " StrSplit(file, "\").Pop()
 If Instr(FileExist(file), "D")
      FileCopyDir, %file%, %target%
 Else FileCopy   , %file%, %target%
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while copying the file.`n`n%file%
}

Msgbox , , Super, copy finished, 2 

ExitApp

I spent a lot of hours today to try to mix this two codes and to solve my last problem to finish my global program of Quick Backup. Unfortunately, I'm not enough good in Autohotkey (I'm not a professional of the programmation).

Perhaps somebody can help me to finish this last step ?

Thank you very much in advance for your kind help.

Best regards,

Nicolas.
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

10 Nov 2021, 07:21

Code: Select all

^1::Run, % A_AhkPath " " A_ScriptDir "\backup.ahk " GetSelectedExplorerOrDesktop()
#IfWinActive ahk_class FreeCommanderXE.SingleInst.1 ahk_exe FreeCommander.exe
^1::Send ^2
#IfWinActive

GetSelectedExplorerOrDesktop() {
 hwnd := WinExist("A")
 WinGetClass, class, ahk_id %hwnd%
 If (class ~= "Progman|WorkerW")
  Return JEE_ExpGetSelDesktop(" ")
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection .= (selection ? " " : "") """" item.path """"
 Return selection
}

JEE_ExpGetSelDesktop(vSep := "`n") { ; Get paths of selected desktop files
 ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
 oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
 oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 vCount := oWin.Document.SelectedItems.Count
 VarSetCapacity(vOutput, (260 + StrLen(vSep)) * vCount * 2)
 For oItem in oWin.Document.SelectedItems
  selection .= SubStr(oItem.path, 1, 3) = "::{" ? "" : """" oItem.path """" vSep
 Return SubStr(selection, 1, -StrLen(vSep))
}
backup.ahk:

Code: Select all

If !FileExist(dest := "D:\Backup") {
 FileCreateDir, %dest%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  Return
 }
}
tmstmp := SubStr(A_YYYY, 3) A_MM A_DD "-" A_Hour A_Min, n := 0
For each, file in A_Args {
 SplitPath, file,,, ext
 If (ext = "lnk")
  FileGetShortcut, %file%, file
 target := dest "\" tmstmp " " StrSplit(file, "\").Pop()
 If Instr(FileExist(file), "D")
      FileCopyDir, %file%, %target%
 Else FileCopy   , %file%, %target%
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while copying the file.`n`n%file%
 Else n++
}
MsgBox, 64, Done, Files copied: %n%
ExitApp
image211110-0719-001.png
FreeCommander toolbar setup
image211110-0719-001.png (68.61 KiB) Viewed 1747 times
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

10 Nov 2021, 07:53

Really thank you very much for your help.

I will install your program and make the test this evening and I will come back to you to confirm you (but I look very quickly your code and it's look to be just what I neeed).

Best regards

Nicolas.
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

11 Nov 2021, 19:32

Hello Mikeyww,

First of all, thank you for your last code and windows of explanation. I make the test yesterday evening. In theory it need to work. But for a reason that I don't understand, it doesn't work.

Today, I make more deep tests and I isolate the problem. I hope you will help me to find the solution.

The following code works good, and send back a Message Box, with the full path of all the selected files/directories in "the File Manager of Windows" and also on the "Desktop".

Code: Select all

^w::

; THis MsgBox work good
MsgBox, % GetSelectedExplorerOrDesktop()


; CODE WITH PROBLEM WHICH DOESN'T WORK MORE. WHY ????

;~ dest := "D:\Backup"
;~ ;tmstmp := SubStr(A_YYYY,3) . A_MM . A_DD "-" A_Hour . A_Min . A_Sec                ; 211108-173512
;~ tmstmp := SubStr(A_YYYY,3) . A_MM . A_DD "-" A_Hour . A_Min                         ; 211108-1735
;~ for a,b in GetSelectedExplorerOrDesktop()
	;~ {
	;~ if instr(FileExist(b),"D")
		;~ FileCopyDir, %b%, % dest "\" tmstmp " " strsplit(b,"\").pop()
	;~ else 
		;~ FileCopy, %b%, % dest "\" tmstmp " " strsplit(b,"\").pop()
	;~ }
;~ return


GetSelectedExplorerOrDesktop() {
 hwnd := WinExist("A")
 WinGetClass, class, ahk_id %hwnd%
 If (class ~= "Progman|WorkerW")
  Return JEE_ExpGetSelDesktop(" ")
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection .= (selection ? " " : "") """" item.path """"
 Return selection
}

JEE_ExpGetSelDesktop(vSep := "`n") { ; Get paths of selected desktop files
 ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
 oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
 oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 vCount := oWin.Document.SelectedItems.Count
 VarSetCapacity(vOutput, (260 + StrLen(vSep)) * vCount * 2)
 For oItem in oWin.Document.SelectedItems
  selection .= SubStr(oItem.path, 1, 3) = "::{" ? "" : """" oItem.path """" vSep
 Return SubStr(selection, 1, -StrLen(vSep))
}

But the following code doesn't work. It doen't make the copy of the files/directory in D:\Backup :

Code: Select all

^w::

; THis MsgBox work good
; MsgBox, % GetSelectedExplorerOrDesktop()


; CODE WITH PROBLEM WHICH DOESN'T WORK MORE. WHY ????

dest := "D:\Backup"
;tmstmp := SubStr(A_YYYY,3) . A_MM . A_DD "-" A_Hour . A_Min . A_Sec                ; 211108-173512
tmstmp := SubStr(A_YYYY,3) . A_MM . A_DD "-" A_Hour . A_Min                         ; 211108-1735
for a,b in GetSelectedExplorerOrDesktop()
	{
	if instr(FileExist(b),"D")
		FileCopyDir, %b%, % dest "\" tmstmp " " strsplit(b,"\").pop()
	else 
		FileCopy, %b%, % dest "\" tmstmp " " strsplit(b,"\").pop()
	}
return


GetSelectedExplorerOrDesktop() {
 hwnd := WinExist("A")
 WinGetClass, class, ahk_id %hwnd%
 If (class ~= "Progman|WorkerW")
  Return JEE_ExpGetSelDesktop(" ")
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection .= (selection ? " " : "") """" item.path """"
 Return selection
}

JEE_ExpGetSelDesktop(vSep := "`n") { ; Get paths of selected desktop files
 ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
 oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
 oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 vCount := oWin.Document.SelectedItems.Count
 VarSetCapacity(vOutput, (260 + StrLen(vSep)) * vCount * 2)
 For oItem in oWin.Document.SelectedItems
  selection .= SubStr(oItem.path, 1, 3) = "::{" ? "" : """" oItem.path """" vSep
 Return SubStr(selection, 1, -StrLen(vSep))
}

I don't manage to understand why. It's not normal.

Do you have perhaps an idea ? Thank you in advance.

Best regards,

Nicolas.
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

11 Nov 2021, 19:50

It's because you mixed two different scripts together. My last post shows two scripts. The functions return text strings rather than arrays, so that the backup script can execute properly. The backup script accepts command-line parameters, which are text strings. As a starting point, I recommend that you run the exact script from my last post, and also use the backup.ahk script provided there, so that you can determine whether those scripts work. If they do work, you can then modify them, although these scripts already provide the backup in all three of your scenarios. Perhaps you see what I mean. I am suggesting that if you start with these working scripts, it might be easier and faster for you to make modifications from there, but this is up to you. An alternative is to use the older functions that return arrays. I provided the newer functions because they make all three of your scenarios work with a single backup script.
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

11 Nov 2021, 19:53

I'm not sure I understand good what do you mean. I will make new tests and will come back soon by trying to use only your codes, without to change it. I will come back soon to you. Thank you.
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

11 Nov 2021, 20:26

In fact, I try first with your 2 scripts (without to change nothing), but it doen't work.

This is why, I decide to make some tests by myself .... also without to have results !

I make now a new test. I will explain you exactly what I do:

I create the file "C:\TEST\Backup.ahk", where I put your code:

Code: Select all

If !FileExist(dest := "D:\Backup") {
 FileCreateDir, %dest%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  Return
 }
}
tmstmp := SubStr(A_YYYY, 3) A_MM A_DD "-" A_Hour A_Min, n := 0
For each, file in A_Args {
 SplitPath, file,,, ext
 If (ext = "lnk")
  FileGetShortcut, %file%, file
 target := dest "\" tmstmp " " StrSplit(file, "\").Pop()
 If Instr(FileExist(file), "D")
      FileCopyDir, %file%, %target%
 Else FileCopy   , %file%, %target%
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while copying the file.`n`n%file%
 Else n++
}
MsgBox, 64, Done, Files copied: %n%
ExitApp

I create the file "C:\TEST\Make the Backup from Desktop - File Manager - Free commander.ahk", whre I put your code:

Code: Select all

^1::Run, % A_AhkPath " " A_ScriptDir "\backup.ahk " GetSelectedExplorerOrDesktop()
#IfWinActive ahk_class FreeCommanderXE.SingleInst.1 ahk_exe FreeCommander.exe
^1::Send ^2
#IfWinActive

GetSelectedExplorerOrDesktop() {
 hwnd := WinExist("A")
 WinGetClass, class, ahk_id %hwnd%
 If (class ~= "Progman|WorkerW")
  Return JEE_ExpGetSelDesktop(" ")
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection .= (selection ? " " : "") """" item.path """"
 Return selection
}

JEE_ExpGetSelDesktop(vSep := "`n") { ; Get paths of selected desktop files
 ; https://www.autohotkey.com/boards/viewtopic.php?p=154836#p154836
 oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
 oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
 vCount := oWin.Document.SelectedItems.Count
 VarSetCapacity(vOutput, (260 + StrLen(vSep)) * vCount * 2)
 For oItem in oWin.Document.SelectedItems
  selection .= SubStr(oItem.path, 1, 3) = "::{" ? "" : """" oItem.path """" vSep
 Return SubStr(selection, 1, -StrLen(vSep))
}
I put the following parameters in FreeCommander:

image.png
image.png (122.7 KiB) Viewed 1633 times

I launch the program: "Make the Backup from Desktop - File Manager - Free commander.ahk"

When i make CTRL + 1, nothing happen (not from the File Manager of windows, not from the Desktop, not from a Windows FreeCommander). When I make CTRL + 2, from the Windows FreeCommander, the program make the copy to D:\Backup.

I don't understand what I make bad ?

Please could you help me.

I will go to sleep now (it's 2:24 in the morning in Belgium) and look your answer tomorrow.

Thank you in advance,

Best regards,

Nicolas
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

11 Nov 2021, 20:34

Thank you for this clear description of your update. I followed your steps and pasted the scripts into new files; it worked at this end.

Your report raises the question of whether your "make" script is running at all, and whether the hotkey triggers. You could test that by replacing "Run" with "MsgBox" on the first line.

Code: Select all

^1::MsgBox, % A_AhkPath " " A_ScriptDir "\backup.ahk " GetSelectedExplorerOrDesktop()
When you test that, you should have the script running but no other scripts running at the same time. My output is below.

image211111-2037-001.png
Output of test "make" script with MsgBox on the first line
image211111-2037-001.png (8.74 KiB) Viewed 1625 times

It appears that with the "Run" in place, the hotkey is not being triggered, because you should get either a MsgBox from the backup script, or some type of AHK error message. If you did not save and reload your script, it is possible that you are running an older version of it.
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

11 Nov 2021, 20:59

Yes ... your idea was genial ... the shortcut ctrl+1 doesn't work !!!! I don't understand why ? But Ok, it's not so important.

Ok, now that I understand the problem ... tomorrow I will change the shortcut and make a new test. I suppose that it will work. I will send you my feedback tomorrow. Now, I'm obliged to go to sleep, because tomorrow I need to work and it's stay me only a few hours to sleep
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 18:07

Hello,

The program was not started with the shortcut ctrl + 1 and ctrl + 2. I don't understand why exactly ? After a lot of test, I decide to change the shortcut by Ctrl + j and Ctrl + p (that I change in Freecommander also) ... and now, it's work very good in all the 3 cases.

I will continue to make some tests a little later and confirm you after this that everything is good.

Thank you very much for your help.

Best regards,

Nicolas.
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 20:08

Good. My guess would be that Ctrl+1 is being used by one of your programs.
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 20:11

Hello,

I confirm you that your code work very good in the 3 cases (but I was only obliged to change the shortcuts ctrl + 1 and ctrl +2, which doen't work good with my configuration ... I don't know why, but in fact it's not important).

I am really very grateful for your help.

I still have a very last idea of ​​something that could be very useful in my work. The idea is to create a directory with all the links of the files/directories that are important in the study of a specific work. The advantage is that we don't have to move the files and directories which concern a job, even if they are disseminated on all the hard disk.

To do that, it's in theory very simple. It's necessary to recuperate your code and to change the "Copy" by "FileCreateShortcut" ... but, when I try to do it, it doesn't work !

To be very clear: I need that every time that I select some files/folders (in exactly the 3 cases of before), the new program, in place to make a copy, will simply send the links of this files/folders to the directory M:\Links.

Please could you help me to adapt a little your code.

Thank you very much in advance.

Best regards,

Nicolas
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 20:16

To answer to your last post that I read only now ...

The problem was also with ctrl + 2, 3 , 4, 5 etc....

When I will have a little free time, I will make new tests, but for me the most important is that your code works good and this super !
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 20:17

My guess is that FreeCommander is actually using all of those hotkeys, but I did not verify that.

Your new post reflects a different question and a different script. Feel free to post your script to get feedback about it. It seems that you would not need a timestamp and would not need to copy any files. You would simply create a shortcut for each file or folder. Example
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 20:50

Thank you for your answer.

The program that I would like to have is the following:

I select some files and/or folders in the "desktop of windows" or "the file manager of windows" or "the FreeCommander file manager". I launch a program which create the shortcuts (links *.lnk) of the selected files/folders in the folder M:\Links.

I don't need more a timestamp and also not to copy the files/directory ... but only create the links (file *.lnk) of the selected files/folders.

I will send you in an half hour, the code that I try and which doesn't work .... and I'm sure you will find me easily the solution (because it's only necessary to adapt a little your code).

Nicolas.
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 21:03

Code: Select all

^F3::Run, % A_AhkPath " " A_ScriptDir "\links.ahk " GetSelectedExplorerOrDesktop()
links.ahk:

Code: Select all

If !FileExist(dest := "M:\Links") {
 FileCreateDir, %dest%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  Return
 }
}
For each, file in A_Args {
 SplitPath, file,,, ext, fnBare
 If (ext = "lnk") {
  FileCopy, %file%, %dest%
  If ErrorLevel
   MsgBox, 48, Error, An error occurred while copying the file.`n`n%file%
 } Else FileCreateShortcut, %file%, %dest%\%fnBare%.lnk
}
MsgBox, 64, Done, Done!
ExitApp
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

12 Nov 2021, 23:14

Hello,

Thank you very much for your code to create and make the copy of the links. I just install it now and it works really fine. It's just what I needed.

I will use your programs every day.

Thank you.

Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

28 Nov 2021, 20:31

Hello,

I've a new question about another subject. I don't know if I need to open a new subject or not ?

**********************************************************************************************************************************************

I begin to study the possibilities to use the Menu of differents programs from AHK.

I manage to do it with the notepad ((Bloc-notes in french).

SetTitleMatchMode, 2
!a::
PostMessage, 0x0111, 33, 0, , Bloc-notes

When I change (by trying) the 33 by other number, I manage to find all the possibilities of the menu of the Notepad.

It take time, but it's possible to find all the possibilities !

**********************************************************************************************************************************************

I try to make the same with Excel (by changing the value 51 by other values) - but in this case nothing happen:

SetTitleMatchMode, 2
!a::
PostMessage, 0x0111, 51, 0, , "ahk_class XLMAIN"

Do you have an idea if PostMessage work or not with Excel ? If yes, is it possible to find on internet a list with all this possibilities of number ?

Thank you very much in advance for your help,

Best regards,

Nicolas.
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

Re: How to retrieve the full adress of a selected file form the Windows File Manager

28 Nov 2021, 20:41

Hello,

I've another little question.

I work with the same computer with two different screen (Monitor Dell and Monitor LG), with the same screen resolution (but one screen is 24inch and one 27 inch). One of the monitor is at home, and one in the office (my computer is "minibox" easy to move.

I would like to use the same AHK programm, but I need to change a parameter in the program, depending which monitor I use. I need then the name of the Monitor (or every parameter which give the possibility to understand which monitor I'm using).

I try:

Code: Select all

SysGet, OutputVar, MonitorName
msgbox %OutputVar%

Infortunately the Msgbox show me the same answer " //./Display1" for the two Monitors.

How is it possible to make the distinguish between the two monitors ?

Thank you very much for your kind help.

Best regards,

Nicolas Guttmann.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: skeerrt and 133 guests