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
Nicolas25
Posts: 39
Joined: 22 Oct 2021, 09:17

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

Post by Nicolas25 » 07 Nov 2021, 19:37

Hello,

When I select a file (or folder) in the Windows file manager, (WIN 10), I would like to launch a MakeACopy.ahk program which makes a copy of this file directly in the directory D:\Backup.

For this, I have to retrieve the full address of the selected file (or folder) and then make a copy (with FileCopy).

I don't know how to retrieve the full address of the selected file?

Thank you very much for your help.

Best regards.

Nicolas.

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

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

Post by mikeyww » 07 Nov 2021, 20:19

Welcome to AHK!

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

Code: Select all

backupDir = D:\Backup

#IfWinActive ahk_class CabinetWClass
F3::
For each, file in Explorer_GetSelection() {
 FileCopy, %file%, %backupDir%
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while copying the file.`n`n%file%
}
MsgBox, 64, Done, Done!
Return
#IfWinActive

Explorer_GetSelection() {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 WinGetClass, winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (window.HWND && hWnd = window.HWND && shellFolderView := window.Document)
    Break
 } Else shellFolderView
     := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}

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

Post by Nicolas25 » 07 Nov 2021, 21:03

Thank you very much for your help. It's really help me a lot.

Your program works very good when I make the copy of one or more files to the directory D:\Backup.

But if I make the copy of a directory, your program copies all the files of this directory to D:\Backup (but not the directory itself).

If I select files, your program is just what I need.

But if I select a directory, I would like to have in D:\Backup the copy of this directory (with all the sub-directory and files).

It's an easy way to make a backup quickly, when we work on new projet, of just what we need (like files or directory).

Thank you very much in advance for your help.

Best regards,

Nicolas

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

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

Post by flyingDman » 08 Nov 2021, 00:00

Try this:

Code: Select all

#\::
dest := "D:\Backup"
for a,b in GetSelected()
	{
	if instr(FileExist(b),"D")
		FileCopyDir, %b%, % dest "\" strsplit(b,"\").pop()
	else 
		FileCopy, %b%, %dest%
	}
return

GetSelected()
	{
	hwnd := WinExist("A"), 	selection := []
	WinGetClass class, ahk_id %hwnd%
	if (class ~= "(Cabinet|Explore)WClass") 
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				for item in window.document.SelectedItems
					selection.Push(item.Path)		
	return selection
	}
I have assumed that if you want to backup a folder called c:\data\spreadsheets\project123 that the backup folder would be d:\backup\project123.

re: pop() see here: https://www.autohotkey.com/docs/objects/Object.htm#Pop
14.3 & 1.3.7

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

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

Post by mikeyww » 08 Nov 2021, 05:11

That's a cool one! Thanks, flyingDman.

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

Post by Nicolas25 » 08 Nov 2021, 11:53

Hello FlyingDman,

First of all, thank you very much for your code and thank you very much also for the code of Mikeyww. You are really very gifted in programmation of Autohotkey.

It's just what I needed. It gives me the possibility to choose one or more directories and/or files and to copy everything (in one operation) to the directory D:\Backup. It's really very useful and give the possibilities to make backups very quickly (and then more often). I'm sure it can help a lot of people.

I've made just now a lot of test of your program and I understand that it miss a little something. It will be very convenient to have the possibility to make a few backup of the same files (and/or directories) the same day (by keeping in the directory D:\Backup the differents versions).

The solution will be to add the date and hour, juste before the name of the files (or directories) copied (I go in supposition, that I will not make two backup the same minut, then the seconds are not necessary- but for other people it's perhaps interesting).

To be more clear:

I would like that the files (or directories) copied will have in the begin the date and hour in the following format YYMMDD-HHMM (YY for the year in format two digits, M for the month in format two digits, D for the day in format two digits, H for the hour in format two digits and 24h by day, MM for minutes in format two digits).

By exemple:

I go to the File Manager, and I select in the directory C:\Computer, the following three files and two directories:

C:\Computer\informations.xlsx
C:\Computer\autohotkey explanations.pdf
C:\Computer\help autohotkey.doc

C:\Computer\materials
C:\Computer\programs

After this I launch the code *.ahk

The result will be (if I Iaunch the code at 17:35 the 08 november 2021):

D:\Backup\211108-1735 informations.xlsx
D:\Backup\211108-1735 autohotkey explanations.pdf
D:\Backup\211108-1735 help autohotkey.doc

D:\Backup\211108-1735 materials
D:\Backup\211108-1735 programs

Remark: all the files inside the two repertories copied "materials" and "programs", don't need to have the date and hour informations (it goes without saying).

I try to be the most clear possible, but if not don't hesitate to ask me more informations.

Thank you very much in advance for your kind help.

Best regards,

Nicolas Guttmann.






[/b]

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

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

Post by mikeyww » 08 Nov 2021, 13:33

You can prepend some date variables to your destination.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

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

Post by flyingDman » 08 Nov 2021, 13:59

Code: Select all

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

GetSelected()
	{
	hwnd := WinExist("A"), 	selection := []
	WinGetClass class, ahk_id %hwnd%
	if (class ~= "(Cabinet|Explore)WClass") 
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				for item in window.document.SelectedItems
					selection.Push(item.Path)		
	return selection
	}
14.3 & 1.3.7

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

Post by Nicolas25 » 08 Nov 2021, 15:10

Hello FlyingDman,

Thank you very much. I made just now a lot of tests with your new code and it's work really great. You are genial !

It's stay me a last question to finish this program. Sometimes I use also the program "FreeCommander XE" in place of the native 'Windows File Manager" (in fact I use the two, depending what I need to do).

I made a test and your code doen't work with "FreeCommander XE" (it's normal, I spoke in the begin only about Windows File Manager)

I use the Window Spy of Autohotkey when the "FreeCommander XE" windows was open. You will find the screen copy in attachments.

Could you please help me to add in your last code, the possibility to use also the "FreeCommander XE" program.

Thank you very much for your kind help.

Best regards,

Nicolas.
Attachments
Windows Manager.JPG
windows spy of the Windows File Manager
Windows Manager.JPG (89.79 KiB) Viewed 1636 times
FreeCommander XE.JPG
windows spy of the FreeCommander XE
FreeCommander XE.JPG (90.26 KiB) Viewed 1636 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

Post by Nicolas25 » 08 Nov 2021, 15:19

I also forget to ask you in my last post, to add also the possibility to make a copy of files (and/or) directories directly from the icons of the desktop (and not only when C:\Users\Nicolas\Desktop is open in the File Manager).

Thank you very much.

Nicolas.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

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

Post by flyingDman » 08 Nov 2021, 15:29

Could you please help me to add in your last code, the possibility to use also the "FreeCommander XE" program.
That's not going to work. Maybe someone else knows.
My GetSelected() function only works with Windows Explorer. Other similar functions work with both Desktop and Explorer. (I don't not use the desktop for data; I have no need for that).
14.3 & 1.3.7

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

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

Post by mikeyww » 08 Nov 2021, 15:33

Just an example with Total Commander: the program can be configured to pass the selected file paths to any program of your choice, such as a batch file or AHK script. The name of this feature is the start menu. I have a couple of dozen batch files and AHK scripts that are set up to do different things using that start menu. It is quite convenient. For example, I have scripts that move files, convert to PDF, open files (as many as I select), and rename files. It's possible that other file managers can do this or have some internal variable that can be used to do 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

Post by Nicolas25 » 08 Nov 2021, 16:06

Thank you very much flyingDman for your help.

I don't undertand exactly Mikeyww, when you tell: "the program can be configured to pass the selected file paths to any program of your choice, such as a batch file or AHK script." ?

It's possible to recuperate without problem in "Total Commander" the path of the selected file (or directory) in the clipboard, but it doen't help me. In fact, I would like that the program *.ahk recuperate alone directely this information when I select the file (and after this make me the copy to my directory D:\Backup)

Please could you give me more informations.

Thank you very much in advance.

Nicolas.

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

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

Post by mikeyww » 08 Nov 2021, 16:12

You mean you want to copy paths to the clipboard, or you want to use TC's start menu instead? Actually, it doesn't matter, because you can assign hotkeys to the TC start-menu items. Is that what you are trying to do-- use a TC file selection to back up those files?

Code: Select all

If !FileExist(dest := "D:\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%
}
ExitApp
image211108-1628-001_cr.png
Total Commander Start Menu configuration (example)
image211108-1628-001_cr.png (8.17 KiB) Viewed 1517 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

Post by Nicolas25 » 08 Nov 2021, 18:13

Thank you for your code and proposition Mikeyww.

Ok, now I begin to understand a little better. But the problem is that I would like to use the program "Free Commander XE" (and not "Total Commander"). In "Free Commander XE", I don't know if they have this option "START". I need to control.

If you ask me why I use "Free Commander XE" and not "Total Commander": The reason is that I use the excellent program "Quick access Popup" (that you probably know) and "Quick access Popup" has some features that are important for me (and that are compatible only with some File Manager programs: "Free Commander XE" is one of them and it's also a very good File Manager).

Ok, it's late (in Belgium) and I will continue to try to understand tomorrow ...

What I try to have in fact, it's exactly the same program that flyingDman write me, but for "Free Commander XE" and also for the "Desktop" of Windows. I need in fact to have the same type of function GetSelected() but for "Free Commander XE" and the "desktop" of windows.

Best regards,

Nicolas.

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

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

Post by mikeyww » 08 Nov 2021, 19:00

It works there.

Tools -> Favorite tools -> Favorite tools edit -> Add new toolbar -> Test -> Add new item -> Backup

You can set a hotkey there as well.

%ActiveSelOrDir% = Full path of the active panel's selected files and directories

image211108-1859-001.png
Customize a FreeCommander XE toolbar to run your AHK script
image211108-1859-001.png (68.36 KiB) Viewed 1466 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

Post by Nicolas25 » 08 Nov 2021, 22:04

Hello mikeyww,

I will make the test later this afternoon, but I examine and try to understand your code just now.

If I understand good the %ActivSelOrDir% put in memory all the selected file(s) (and/or directory(ies)) paths. This informations are passed like parameters to the array A_Args of the program D:\temp2\s.ahk.

Please could you confirm me that I understand good ?

Thank you in advance.

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

Post by Nicolas25 » 08 Nov 2021, 22:18

Thank you very much. I will try later and give you the result.

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

Post by Nicolas25 » 09 Nov 2021, 07:53

Hello mikeyww,

I just made the test of what you propose me in your last post for "Free Commander XE". It work very good. Thank you very much for your important help.

Now I have 2 programs, one for "Free Commander XE" and one for the native "File Manager" of Windows.

It stays only one case, for which I will be very happy to find a solution: it is the possibility to make exactly the same Backup directly from the Desktop of Windows 10 (without to be obliged to open in the File Manager C:\Users\Nicolas\Desktop).

I found on your site a topic which speak about the question of the Desktop:

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

Unfortunately, my knoledge in Autohotkey are basic (but I continue to study when I've free time) and I don't understand everything in this topic. If you have a little free time, please could you help me to write the same type of program, but to make the copies of files/directories directly from the Desktop.

After this, I will try to put the three options together in one program (probably with the #IfWinActive).

Thank you very much in advance for your help.

Best regards,

Nicolas.

Post Reply

Return to “Ask for Help (v1)”