1

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.

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.

  • This looks promising, but I'm looking for a solution with only CTRL+C as user interaction. (No drag/drop). The rest should be done only by the .bat batch file. – Basj Sep 3 '18 at 23:02
  • if you run a batch file then you already have to drag and drop those files onto the batch file in explorer, or type the batch file name then drag and drop onto cmd. I don't understand what you mean by "how to get its filename in a batch file test.bat" in this case. If you want to paste those names into the batch file content then it's useless, otherwise to pass them as arguments do like I mentioned – phuclv Sep 4 '18 at 3:41
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.

  • I know this trick, but I'm looking for a solution that works with the standard CTRL+C (and no F2). Moreover, I'm looking for the full path. – Basj Sep 3 '18 at 22:37
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.

  • Thanks for your answer. As mentioned in the question, I'm looking for a solution with CTRL+C and then available later in a .bat batch file via command line (without any drag/drop file to cmd.exe). – Basj Sep 3 '18 at 23:01
  • see this link on how to then paste into command line: howtogeek.com/howto/25590/… – WireGuy Sep 3 '18 at 23:05
  • Thanks @WireGuy, I already use this. But in my context, the batch is not even displayed, so I shouldn't have to see the cmd.exe to paste there manually. – Basj Sep 3 '18 at 23:07
  • To see why I have these strange requirements, here is a solution using Python: superuser.com/questions/260740/… for a well known problem (create symlink with GUI in Windows). I was trying to do the same with a batch only (no Python), but for this I need to be able to get the filename in a batch ;) That's the whole story! Thanks by the way for your help! – Basj Sep 3 '18 at 23:08
  • @Basj just drag and drop the files to the batch file, no need for the cmd to be displayed. Otherwise how do you intend to paste those file names? – phuclv Sep 4 '18 at 3:40
0

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.

  • I get this in Windows 7: The term 'Get-Clipboard' is not recognized as the name of a cmdlet, function. – Basj Sep 4 '18 at 6:56
  • Get-Clipboard is a new cmdlet in PowerShell 5.0. Windows 7 includes PowerShell 2.0 which is too old to be useful – phuclv Sep 4 '18 at 14:07
  • @Basj As phuclv pointed out, you need more recent PowerShell version and at least .NET Framework 4.5 or above – Alex Sep 4 '18 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.