Get filename after a CTRL+C on a file with Windows Explorer
Asked Answered
A

3

9

When you do Copy (CTRL+C) on a file, then in some programs (example: it works in the Windows Explorer address bar, also with Everything indexing software), when doing Paste (CTRL+V), the filename or directory name is pasted like text, like this: "d:\test\hello.txt".

I tried this:

  • CTRL+C on a file or folder in Windows Explorer
  • Run:

    import win32clipboard
    win32clipboard.OpenClipboard()
    data = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    print data
    

But I get this error:

TypeError: Specified clipboard format is not available

Question: how to retrieve the filename of a file that has been "copied" (CTRL+C) in the Windows Explorer?

Arv answered 3/9, 2018 at 21:45 Comment(0)
U
11

The clipboard may contain more than one format. For example, when formatted text is copied from MS word, both the formatted text and the plain text will be in the clipboard, so that depending on the application into which you are pasting, the target application may take one or the other format, depending on what it supports.

From MSDN:

A window can place more than one clipboard object on the clipboard, each representing the same information in a different clipboard format. When placing information on the clipboard, the window should provide data in as many formats as possible. To find out how many formats are currently used on the clipboard, call the CountClipboardFormats function.

Because of that, win32clipboard.GetClipboardData takes one argument: format, which is by default win32clipboard.CF_TEXT.

When you call it without arguments, it raises error saying TypeError: Specified clipboard format is not available, because TEXT format is not in the clipboard.

You can, instead, ask for win32clipboard.CF_HDROP format, which is "A tuple of Unicode filenames":

import win32clipboard
win32clipboard.OpenClipboard()
filenames = win32clipboard.GetClipboardData(win32clipboard.CF_HDROP)
win32clipboard.CloseClipboard()
for filename in filenames:
    print(filename)

See also MSDN doc for standard clipboard formats

Unbelt answered 3/9, 2018 at 22:7 Comment(1)
Thanks to your solution, I made this 10-lines of code solution to create symlinks with GUI in Windows Explorer with a simple CTRL+C on target + Right-click on folder where you want to drop the link + Mklink here! :)Arv
E
4

This worked for me:

import win32clipboard
win32clipboard.OpenClipboard()
filename_format = win32clipboard.RegisterClipboardFormat('FileName')
if win32clipboard.IsClipboardFormatAvailable(filename_format):
    input_filename = win32clipboard.GetClipboardData(filename_format).decode("utf-8")
    print(input_filename)
win32clipboard.CloseClipboard()

That prints the whole file path, if you want just the file name use:

os.path.basename(input_filename)
Esta answered 3/9, 2018 at 21:57 Comment(0)
H
0

Try to use this argument >>> CF_UNICODETEXT like this win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) It's work for me. Refer: https://learn.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats

Hypesthesia answered 18/5, 2022 at 8:33 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Abb
CF_UNICODETEXT clipboard format does not work on copying paths as OP wants. To copy file/folder paths into the clipboard, you have to use CF_HDROP.Transcalent

© 2022 - 2024 — McMap. All rights reserved.