2

When you do Copy (CTRL+C) on a file or folder, then in some programs, but not all (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\Temp".

Question: If you do CTRL+C on a file or folder in Windows Explorer, how to get its filename in a batch file test.bat that you run just after?

I tried with clip.exe, as suggested in Access clipboard in Windows batch file , but it did not work : clip.exe seems to do the opposite (write to clipboard).


Note: unlike How to copy file path to clipboard? (its answers require third party software, or different hotkey solution, or use AutoHotkey), here I'm explicitely looking for a solution that works in batch, with the standard CTRL+C (and not another hotkey, not using AutoHotkey, etc.) and no third party software.

Basj
  • 2,143

4 Answers4

1

You can get the full path name by dragging that file from the explorer to the command window, this will insert the full path.

A program can place the content in different types into the clipboard. For instance, Word may place text as plain text and as formatted text. Some programs are clever and extract the file name when you have placed a (reference to a) file into the clipboard. Unfortunately the command windows doesn't.

RalfFriedl
  • 1,734
1

Use powershell that present on all supported Windows versions instead of legacy cmd shell.
(That promised by Microsoft to be removed in a future).

Run powershell and use function:

Get-Clipboard

it will return you content of clipboard, while Set-Clipboard will do opposite - put something in the clipboard.

If you don't want to switch to PowerShell, you can call it on demand from your batch script as:

 powershell -Command "& {Get-Clipboard}"

to get content of clipboard in your script.

Alex
  • 6,375
0

Select a file or folder, press F2, then use Ctrl-C. That should let you just copy the name (really the 'basename') of the file or folder.

NOLFXceptMe
  • 1,342
0

You can Shift+Right click on the file in Explorer and select "Copy as path": enter image description here

Or if you select the file name, and use Ctrl+Shift+C.

This will tell Explorer to copy the information to the clipboard in the CF_TEXT format, instead of the usual CF_HDROP format. Some programs do not know what to do with the CF_HDROP format, but all should understand CF_TEXT format.

WireGuy
  • 1,719