Copy file name and file type Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JMD1

Copy file name and file type

26 Feb 2018, 16:04

I would like to copy a file name and the file type to the clipboard. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard. I am new to this and tried the script below, it doesnt work very well. Thanks.

F2::Send {F2}^a::^c
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

26 Feb 2018, 16:31

Try this - just highlight the file (single click, not doubleclick) and press F2:

Code: Select all

F2::
Clipboard := 					; empty clipboard
Send ^c							; copy via Ctrl+c
Clipwait						; Wait until Clipboard holds contents
SplitPath, Clipboard, name		; get filename - clipboard holds complete file path
msgbox % name					; just for testing
clipboard := name				; assign filename to clipboard for pasting
return
JMD
Posts: 11
Joined: 26 Feb 2018, 15:51

Re: Copy file name and file type

27 Feb 2018, 14:32

gregster wrote:Try this - just highlight the file (single click, not doubleclick) and press F2:

Code: Select all

F2::
Clipboard := 					; empty clipboard
Send ^c							; copy via Ctrl+c
Clipwait						; Wait until Clipboard holds contents
SplitPath, Clipboard, name		; get filename - clipboard holds complete file path
msgbox % name					; just for testing
clipboard := name				; assign filename to clipboard for pasting
return

This works great, just what I wanted. Thanks.
JMD
Posts: 11
Joined: 26 Feb 2018, 15:51

Re: Copy file name and file type

28 Feb 2018, 16:22

gregster wrote:Try this - just highlight the file (single click, not doubleclick) and press F2:

Code: Select all

F2::
Clipboard := 					; empty clipboard
Send ^c							; copy via Ctrl+c
Clipwait						; Wait until Clipboard holds contents
SplitPath, Clipboard, name		; get filename - clipboard holds complete file path
msgbox % name					; just for testing
clipboard := name				; assign filename to clipboard for pasting
return

Hmmm, this worked yesterday but not today. I have reloaded the script several times and even when back and copied the original script again.
When I single click the file name, today, I get a pop-up and the file is not on the clipboard. When I click and highlight the name I get the same pop-up. Please advise. Thanks.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

28 Feb 2018, 16:35

It still works for me - and I don't see why it wouldn't. We are talking about windows' file explorer, right?
Do you have other scripts running? Or several instances? That might cause problems... Then you could add #SingleInstance force at the top of your script (above F2::)

If you are not sure, look at the task manager and count how much Autohotkey.exe processes you are seeing...
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

28 Feb 2018, 17:01

You could also add a timeout to Clipwait, like Clipwait,1 in case the Clipwait gets stuck (can happen, if you hit F2 without having anything highlighted). In case of timeout you would see an empty msgbox (but that would be correct, because nothing was selected).
Without a timeout parameter added, there shouldn't be an empty msgbox (well, in case there are unintentionally multiple script instances running, it could probably appear, I guess)

So, I would add the timeout to Clipwait and remove the msgbox (it was only for testing anyway) - and add #SingleInstance force, of course.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Copy file name and file type

28 Feb 2018, 17:28

Try this

Code: Select all

F2::
Clipboard := ""					; empty clipboard
Send ^c							; copy via Ctrl+c
sleep 50						; <---- this should prevent clipwait issues
Clipwait,1						; Wait until Clipboard holds contents
if (ErrorLevel = 1) 			; Zero if clipboard not empty, else one
	return
SplitPath, Clipboard, name		; get filename - clipboard holds complete file path
msgbox % name					; just for testing
clipboard := name				; assign filename to clipboard for pasting
return
Einfach nur ein toller Typ. :mrgreen:
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Copy file name and file type

01 Mar 2018, 03:21

JMD wrote: Hmmm, this worked yesterday but not today. I have reloaded the script several times and even when back and copied the original script again.
When I single click the file name, today, I get a pop-up and the file is not on the clipboard. When I click and highlight the name I get the same pop-up. Please advise. Thanks.
Plain guessing, but have a try at assigning another key. I've faced the same thing here.

Btw, you can shorten this ...

Code: Select all

msgbox % name					; just for testing
clipboard := name				; assign filename to clipboard for pasting
... to that

Code: Select all

msgbox % clipboard := name				; assign filename to clipboard for pasting
SplitPath() - UDF
Spoiler
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

01 Mar 2018, 03:29

Since the original request was just for loading the clipboard (to be ready for pasting) and the msgbox was just added (by me) for testing, I didn't consider msgbox % clipboard := name a good idea ;) Of course, if the msgbox should stay, then it is possible.

But that last section could still be shortened to SplitPath, clipboard, clipboard :)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Copy file name and file type

01 Mar 2018, 04:40

gregster wrote:Since the original request was just for loading the clipboard (to be ready for pasting) and the msgbox was just added (by me) for testing, I didn't consider msgbox % clipboard := name a good idea ;) Of course, if the msgbox should stay, then it is possible.

But that last section could still be shortened to SplitPath, clipboard, clipboard :)
Ooops. Haven't realized that the code I wanted to refer to wasn't from JMD himself but quoted from you :shifty:
You're damn right in every regard. Thx for your support muchacho :mrgreen:
JMD
Posts: 11
Joined: 26 Feb 2018, 15:51

Re: Copy file name and file type

01 Mar 2018, 08:55

gregster, I do have one other script running so I added #SingleInstance force above F2:: I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the file type. And yes I am using Windows file explorer.

divanebaba, I tried your code because it had the Clipwait,1. I got a msgbox that said filename.ahk

BoBo, I assigned several other F keys. I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the filetype.
I also tried the script you had under Hide button, I got a msgbox that said C:\Program Files\dir\subdir

Thanks for all the help, I am still open to more ideas. :D
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Copy file name and file type

01 Mar 2018, 09:52

One more Idea. Select a file in any explorer window and press q.

Code: Select all

q::
fileselected := Explorer_GetSelection()
SplitPath,fileselected, fn
MsgBox % "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")
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type  Topic is solved

01 Mar 2018, 10:53

I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the filetype.
@JMD, so you are actually looking for the file type - more exactly, what is displayed in the "type" column of file explorer and not (only) for the filename inluding extension (like .ahk or .txt?), right?
I thought by "type" you meant the file extension :shifty:

Something like filename.ahk AutoHotkey Script ? That cannot be done by my script, but I think Odlanir's version could be changed to do that, by accessing theitem.type property of the Shell.Application object.

But first confirm that you are looking for this! Give us an example output!
JMD
Posts: 11
Joined: 26 Feb 2018, 15:51

Re: Copy file name and file type

01 Mar 2018, 11:16

gregster, My bad, wrong choice of words, file extension is what I want. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard.

Odlanir, when I tried your script I got an msgbox that said:
AutoHotke;.ahk
Error at line 43.
Line Text: for window in ComObjCreate("Shel|.App|ication").Window
Error: This line does not contain a recognized action.
The program will exit.

Thanks.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

01 Mar 2018, 11:19

Edit: OK, forget the following, I see you are not interested in file type.

Are looking for something like this? Highlight one file and Hit 'q':

Code: Select all

q::
item := Explorer_GetSelection()
Splitpath, % item.path , filename
MsgBox % filename " " item.type
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 item
}
Note: For clarification purposes, I quickly "doctored" Odlanir's code (sorry, Odlanir) to display the filename with extension, followed by a space and then the "type" from the corresponding file explorer column, so that msgbox output would be something like this: filename.ahk AutoHotkey Script.
Is that what you want, JMD?

Odlanir's original function is actually more sophisticated and can work with more than one selected file. I only mutilated it for demonstration. ;) If that goes in the right direction, the original function could probably be adapted in a much better way.
Last edited by gregster on 01 Mar 2018, 11:39, edited 1 time in total.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

01 Mar 2018, 11:27

JMD wrote:gregster, My bad, wrong choice of words, file extension is what I want. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard.
Well, that is what my script does - complete filename, including extension. At least on my system. For clarification, do you see the file extensions in file explorer or is it disabled via display options? Well, for me it works both ways, anyway, with visible extensions and without. So, that shouldn't be a problem.
The Splitpath docs also seem to confirm me: https://autohotkey.com/docs/commands/SplitPath.htm
OutFileName
Name of the variable in which to store the file name without its path. The file's extension is included.
Odlanir's script should also deliver the data that you want. It should work, I tried it.
In the next step, you will just have to put the filename into the clipboard variable, like in my script. But the error message is a bit strange. Line 43? Can you show your complete script? Make sure you copy the complete code from the code box.

Strange things seem to be going on, on your end ;)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Copy file name and file type

01 Mar 2018, 11:45

Stop! You're only allowed to check this code if you're a bloody noob - and not able to deal with the already provided rocket science! :shifty: :shh:
Spoiler
JMD
Posts: 11
Joined: 26 Feb 2018, 15:51

Re: Copy file name and file type

01 Mar 2018, 12:07

gregster wrote:
JMD wrote:gregster, My bad, wrong choice of words, file extension is what I want. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard.
Well, that is what my script does - complete filename, including extension. At least on my system. For clarification, do you see the file extensions in file explorer or is it disabled via display options? Well, for me it works both ways, anyway, with visible extensions and without. So, that shouldn't be a problem.
The Splitpath docs also seem to confirm me: https://autohotkey.com/docs/commands/SplitPath.htm
OutFileName
Name of the variable in which to store the file name without its path. The file's extension is included.
Odlanir's script should also deliver the data that you want. It should work, I tried it.
In the next step, you will just have to put the filename into the clipboard variable, like in my script. But the error message is a bit strange. Line 43? Can you show your complete script? Make sure you copy the complete code from the code box.

Strange things seem to be going on, on your end ;)

I have other items in my AutoHotkey.ahk file so I am coping your script into notepad. I named it gregster.ahk. So what I click on to activate your script is the only thing in my code box [I think I am using the term code box properly...]. Below is the latest from you and I get an error when I click on the file to activate it.

The error is first, your code is next. Thanks.

Error at line 3.
LineText: % item.path
Error: This parameter contains a variable name missing its ending percent sign.
The program will exit.




q::
item := Explorer_GetSelection()
Splitpath, % item.path, filename
MsgBox % filename " " item.type
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 item
}
JMD
Posts: 11
Joined: 26 Feb 2018, 15:51

Re: Copy file name and file type

01 Mar 2018, 12:11

BoBo wrote:Stop! You're only allowed to check this code if you're a bloody noob - and not able to deal with the already provided rocket science! :shifty: :shh:
Spoiler


BoBo, I have other items in my AutoHotkey.ahk file so I am coping your script into notepad. I named it BoBo.ahk. When I hit F2 it looks like it copies the file name and extension, but when I ctrl-V to paste, it only shows the file name, not the extension. Thanks.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Copy file name and file type

01 Mar 2018, 13:13

@JMD: My guess is that you are using an archaic version of AutohotKey, something like 1.0.x. Am I right?

Windows version would be interesting, too.
(I am using AHK 1.1.28.00 Unicode on Windows 10 64 bit - but Win 7 or 8 should at least be ok, too.)
Last edited by gregster on 01 Mar 2018, 13:15, edited 1 time in total.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, jameswrightesq and 282 guests