Copy & Paste Specific File to Current TC Window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Copy & Paste Specific File to Current TC Window

Post by BeRoM » 17 Aug 2022, 13:03

Hi all!

I am looking for a script to copy and paste a specific file to my Total Commander window at the press of a HotKey. My current script worked beautifully for several years until yesterday, and I cannot figure out why it does not work anymore. I believe I got the script from this archived forum several years ago, but now when I try to run it, it does nothing (possibly loop?).

Here is the current non-working script:

Code: Select all

#IfWinActive, Total Commander ;  this script adds the template file to your current Total Commander Window with Ctrl+Shift+4
^+4::FileToClipboard("\\homeserver\users\ID001\template.dbf")
FileToClipboard(PathToCopy,Method="copy")
   {
   FileCount:=0
   PathLength:=0

   ; Count files and total string length
   Loop,Parse,PathToCopy,`n,`r
      {
      FileCount++
      PathLength+=StrLen(A_LoopField)
      }

   pid:=DllCall("GetCurrentProcessId","uint")
   hwnd:=WinExist("ahk_pid " . pid)
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")
   pPath := DllCall("GlobalLock","UPtr",hPath)
   NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
   NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
   Offset:=0
   Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
      offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2

   DllCall("GlobalUnlock","UPtr",hPath)
   DllCall("OpenClipboard","UPtr",hwnd)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP

   ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
   str := DllCall("GlobalLock","UPtr",mem)

   if (Method="copy")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
   else if (Method="cut")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
   else
      {
      DllCall("CloseClipboard")
      return
      }

   DllCall("GlobalUnlock","UPtr",mem)

   cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
   DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
   DllCall("CloseClipboard")
setwindelay, 2
Send ^v
   return
   }
return
[Mod edit: [code][/code] tags added.]

My goal is to simply press Ctrl+Shift+4 and copy the same template file ("\\homeserver\users\ID001\template.dbf") to my current Total Commander window. Any ideas?

Thank you,

BeRoM :thumbup:

BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Copy & Paste Specific File to Current TC Window

Post by BeRoM » 17 Aug 2022, 13:06

Thanks for the mod-edit to the code window!

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 17 Aug 2022, 14:30

Hi BeRoM

I think it's easier for them to answer you in the Total Commander forum. And that copy... is it always in the same directory? I don't understand why in the current total commander window.

I use it. To reference the current directory of total commander are the variables /O /T "%1" but I don't know how you could integrate that into an Autohotkey script, maybe total_Commander_path\TOTALCMD64.EXE OR /T "%1" and send a send ^v. I've been intigrated, I'm going to find out how to do it.

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Copy & Paste Specific File to Current TC Window

Post by ahk7 » 17 Aug 2022, 14:47

Perhaps you have a specific need for DropFiles but you don't need it as such as you can read the current active path TCs window.
Once you have the path, you can use FileCopy to copy the files.

Code: Select all

#IfWinActive, Total Commander ;  this script adds the template file to your current Total Commander Window with Ctrl+Shift+4
^+4::FileToPanel("\\homeserver\users\ID001\template.dbf")

FileToPanel(files,Overwrite=1)
    {
    if WinActive( "ahk_exe TOTALCMD64.exe" )
		ControlGetText sPath, Window5
	if WinActive( "ahk_exe TOTALCMD.exe" )
		ControlGetText sPath, TMyPanel3
	if ( sPath != "" )
	{
        sPath:=StrReplace(sPath,">","\")
        loop, parse, files, `n, `r
            FileCopy, %A_LoopField%, %sPath%, %Overwrite%
	}
   }
Return

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 17 Aug 2022, 15:09

Hi ahk7.

Have you tried your own script? I've tried it, changing what it copied, and it doesn't do anything. I added MsgBox, %sPath% and it doesn't "say" anything, the message window doesn't come out (with total commander open). I don't know if it's so easy to "interrogate" Total commander to know his current route, from Autohotkey. Simplify the script so that it just says the path.

I found this utility, maybe it will help you, BeRoM: https://luethje.eu/prog/niftylink_en.htm

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Copy & Paste Specific File to Current TC Window

Post by ahk7 » 17 Aug 2022, 15:26

Of course I tried it and it does work and yes it is that easy - I do know my way around TC & AHK ;)
https://github.com/hi5/F4MiniMenu
https://github.com/hi5/TCSyncComments
https://github.com/hi5/qvd

Edit: may be the title match mode doesn't do it for you, and TC must be active when you press the shortcut (just saying)

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 17 Aug 2022, 16:17

Hi ahk7 :wave:

I tried the following script (to simplify the process) and switching the commented parts, sometimes with TOTALCMD.exe and others with TOTALCMD64.exe, it works with TOTALCMD.exe, but with TOTALCMD64.exe it always tells me "Default.bar", regardless of changing directories or HD drives. I use Total commander 10.51RC, I don't know if anything has changed.

Code: Select all

^+F3::
if WinActive( "ahk_exe TOTALCMD64.exe" )
ControlGetText sPath, Window5
;if WinActive( "ahk_exe TOTALCMD.exe" )
;ControlGetText sPath, TMyPanel3
MsgBox, %sPath%
Waiting to see if it works for BeRoM...

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Copy & Paste Specific File to Current TC Window

Post by ahk7 » 17 Aug 2022, 16:27

I checked again with the Window Spy and the classes haven't changed so not sure why it isn't working for you.
Just this script and running it by pressing enter on it in TC shows the path

Code: Select all

if WinActive( "ahk_exe TOTALCMD64.exe" )
ControlGetText sPath, Window5
MsgBox, %sPath%

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 17 Aug 2022, 17:13

Your script is the same script that I have written above. In Classnn it tells me "LCLListBox1", I haven't found any references to "Window5". Where does "Window5" come from, what Windows Spy do you work with?

(I try to learn)

BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Copy & Paste Specific File to Current TC Window

Post by BeRoM » 17 Aug 2022, 17:24

Perhaps I need to clarify how I am using this script:

I use TC everyday. Numerous times a day, I navigate different folders in TC (or create new folders), and I need to add this default template.dbf file. In the past, I would go to the default location of this template file and use Ctrl+C, then go back to my desired folder and use Ctrl+V, and the file would be copied to this location. However, instead of constantly navigating to the default folder, I used the script to instantly copy/paste the template file. Until earlier this week, this was working perfectly... 1) navigate to folder within TC, 2) Press Ctrl+Shift+4, and the template.dbf file was instantly copied/pasted. For some reason, it does not work anymore. I have no idea why it stopped working. Whenever I start the script, I see a small loading cursor, and then nothing happens... no errors.

The default template.dbf file always stays in it's home location: \\homeserver\users\ID001\template.dbf

All this script does is instantly copy/paste the default template.dbf file into my current TC folder.

I am just wondering if there is a simpler code that will allow me to copy/paste this template file whenever I am using TC.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 17 Aug 2022, 17:25

Oh I see! It refers to the control that is under the "\" icon (I think). Looking with UIAViewer.ahk is more accurate. Maybe some extension is interfering?

BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Copy & Paste Specific File to Current TC Window

Post by BeRoM » 17 Aug 2022, 17:34

Ugh. I think it is a Windows 10 issue/change.

The script I posted still works perfectly in Windows 7.

I am open to any ideas.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 18 Aug 2022, 05:09

Try the first ahk7 script.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 18 Aug 2022, 07:57

Hi BeRoM. A simpler version of the ahk7 script and it works for me:

Code: Select all

~^+k:: ; Strange shortcut, but because of the need to find a keyboard shortcut that doesn't use TC
FileTo:="d:\Download\Image_DL\20220719.jpg"

if WinActive( "ahk_exe TOTALCMD.exe" ){
ControlGetText sPath, TMyPanel3
}
if WinActive( "ahk_exe TOTALCMD64.exe" ){
ControlGetText sPath, Window17
}
sPath:=StrReplace(sPath,">","\")
;MsgBox, %sPath% ; Remove the semicolon to check that the script has a path and if it is correct
FileCopy, %FileTo%, %sPath%
sPath:=""
What happens is that the control (the one that appears in the script as TMyPanel3 in one case and Window17 in another) is not the same for all Total Commander configurations, since it lists it depending on whether "HD view units" is enabled, and other issues. To know the name of the control you have to use ahkSpy.ahk (or in .exe) that is downloaded from here: https://github.com/serzh82saratov/AhkSpy/find/master In my case for TC64 it is better to take control than I show on the graph.

Image

Notice where the cursor is in each case.

BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Copy & Paste Specific File to Current TC Window

Post by BeRoM » 18 Aug 2022, 09:21

wetware05 wrote:
18 Aug 2022, 07:57
Hi BeRoM. A simpler version of the ahk7 script and it works for me:
The ahk7 script is not working for me. It does create a file on my desktop (not in my TC directory), but the file itself is corrupted.

Any idea why my original script doesn't work in Win 10 anymore? Does the original script work for you?

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 18 Aug 2022, 10:18

BeRoM wrote:
17 Aug 2022, 13:03
Hi all!

I am looking for a script to copy and paste a specific file to my Total Commander window at the press of a HotKey. My current script worked beautifully for several years until yesterday, and I cannot figure out why it does not work anymore. I believe I got the script from this archived forum several years ago, but now when I try to run it, it does nothing (possibly loop?).

Here is the current non-working script:

Code: Select all

#IfWinActive, Total Commander ;  this script adds the template file to your current Total Commander Window with Ctrl+Shift+4
^+4::FileToClipboard("\\homeserver\users\ID001\template.dbf")
FileToClipboard(PathToCopy,Method="copy")
   {
   FileCount:=0
   PathLength:=0

   ; Count files and total string length
   Loop,Parse,PathToCopy,`n,`r
      {
      FileCount++
      PathLength+=StrLen(A_LoopField)
      }

   pid:=DllCall("GetCurrentProcessId","uint")
   hwnd:=WinExist("ahk_pid " . pid)
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")
   pPath := DllCall("GlobalLock","UPtr",hPath)
   NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
   NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
   Offset:=0
   Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
      offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2

   DllCall("GlobalUnlock","UPtr",hPath)
   DllCall("OpenClipboard","UPtr",hwnd)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP

   ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
   str := DllCall("GlobalLock","UPtr",mem)

   if (Method="copy")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
   else if (Method="cut")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
   else
      {
      DllCall("CloseClipboard")
      return
      }

   DllCall("GlobalUnlock","UPtr",mem)

   cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
   DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
   DllCall("CloseClipboard")
setwindelay, 2
Send ^v
   return
   }
return
[Mod edit: [code][/code] tags added.]

My goal is to simply press Ctrl+Shift+4 and copy the same template file ("\\homeserver\users\ID001\template.dbf") to my current Total Commander window. Any ideas?

Thank you,

BeRoM :thumbup:
If it works, perfectly, better than the other scripts, because it does it in the active panel of Total Commander. But I have changed the copy from a local file, on line 2 to ^+4::FileToClipboard("d:\Download\Image_DL\20220719.jpg") Try to do the same yourself.

I think your problem is if you have configured the local network (or the intranet, i don't know where the resource you want to copy from a network computer is located) in Windows 10. Check the Windows network settings. By default it does not enable access to network resources.

Enable local network resources (The video is not mine):


BeRoM
Posts: 16
Joined: 17 Aug 2022, 12:51

Re: Copy & Paste Specific File to Current TC Window

Post by BeRoM » 18 Aug 2022, 10:27

Based on a recommendation, I placed numerous msgbox commands throughout the code to find where the crash is happening, and it happens after clicking OK on Step 6:

Code: Select all

msgbox step_6
   NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
msgbox step_7
   NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
msgbox step_8
   Offset:=0
   Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
msgbox step_9
      offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2
msgbox step_10
msgbox step_11
   DllCall("GlobalUnlock","UPtr",hPath)
   DllCall("OpenClipboard","UPtr",hwnd)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP
msgbox step_12
   ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
   str := DllCall("GlobalLock","UPtr",mem)
msgbox step_13
   if (Method="copy")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
   else if (Method="cut")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
   else
      {
      DllCall("CloseClipboard")
      return
      }
msgbox step_14
   DllCall("GlobalUnlock","UPtr",mem)

   cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
   DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
   DllCall("CloseClipboard")
msgbox step_15
setwindelay, 2
Send ^v
   return
   }

return

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Copy & Paste Specific File to Current TC Window

Post by ahk7 » 18 Aug 2022, 11:04

@wetware05 5

Tick the "follow mouse" check box and hover over the 'path' control on the left of the command line input, now you should see Window5 or TMyPanel3 depending on 64/32-bit version of TC.

The developer of TC is well aware many people use AutoHotkey and other tools to script TC - as well as accessibility to screenreaders - so tries to ensure everything is "visible" to such tools.

Edit: I see you try to find the path in another control, "my" control in at the bottom of the TC window near the commandline (https://www.ghisler.com/screenshots/en/01.html where it says d:\data\dev> just above the button F3-F* bar)


@BeRoM

If the above script(s) don't work for you, there are more ways to do it:

1. Copy path to clipboard

If you don't mind using the clipboard you can simply copy the current path to the clipboard and use it.

You can store/restore the current clipboard contents - see AutoHotkey clipboard documentation on how to do it, omitted it here:

Code: Select all

#IfWinActive, Total Commander ; this script adds the template file to your current Total Commander Window with Ctrl+Shift+4
^+4::FileToPanel("\\homeserver\users\ID001\template.dbf")
FileToPanel(files,Overwrite=1)
{
clipboard:=""
cm_CopySrcPathToClip=2029 ;Copy source path to clipboard
SendMessage 1075, %cm_CopySrcPathToClip%, 0, , ahk_class TTOTAL_CMD
{
loop, parse, files, `n, `r
FileCopy, %A_LoopField%, %clipboard%, %Overwrite%
}
}
Return
Or more directly

Code: Select all

^+4::
clipboard:=""
cm_CopySrcPathToClip=2029 ;Copy source path to clipboard
SendMessage 1075, %cm_CopySrcPathToClip%, 0, , ahk_class TTOTAL_CMD
FileCopy, %A_LoopField%, %clipboard%, 1
Return
2. A simple batch file.

Create the following batch file and save it as say "ct.cmd" somewhere in your path environment:

Code: Select all

Copy \\homeserver\users\ID001\template.dbf
Now just type 'ct' + Enter in the TC command line and it should copy the file to the current panel/folder.

You could also create a batch file to make the dir, cd into dir and copy the file so you can type
"ct newdir" enter

3. Create a TC button

Right click on the TC button bar, change+add, and add

Command: %comspec% /c Copy \\homeserver\users\ID001\template.dbf
Parameter: %P
Don't forget to select an icon

Now when you press the button it should copy that file to the current panel

4. Create a User Command

If you don't have one already, create a file in your TC INI folder (check Help, About for the path to your totalcmd.ini file)

You can use notepad.exe - The filename must be "usercmd.ini" Add:

Code: Select all

[em_CopyTemplateFile]
cmd=%comspec% /c Copy \\homeserver\users\ID001\template.dbf
param=%P
Restart TC just to be sure. Go to Misc. section in the configuration menu, select a shortcut you'd like to use and search for em_CopyTemplateFile in the list

Or you can add the batch file (2) to the button bar or user command, or assign the usercommand to the button bar.

Finally are you sure you're not running TC as admin and your ahk script as normal user?

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 18 Aug 2022, 11:59

Hi ahk7, you insist on something that is not like that.

In the image below, with two different versions of Total Commander X64, the TCUP version (in dark), very tuned, and the other without any changes. The "window5" control is the button to exit the application in the second, while in the TCUP version, that same button is the "Window7" control. In the TCUP version I haven't been able to find the "Window5" control and I've traced everything very slowly.

Image

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Copy & Paste Specific File to Current TC Window

Post by wetware05 » 18 Aug 2022, 12:15

I have suspected that the center bar (between od two panes), can affect the window assignment and VOILÀ!!!, what was previously the "Window 5" control is now the "Window4", and the "Window5" control is now the one you mean...

Image

Then the number of the controls assigns them dynamically.

Post Reply

Return to “Ask for Help (v1)”