Loading...
Random Pearls Random Pearls New Menu
  • All Pearls
    • Random Pearls
  • Coding and Development - Reference …
    • Coding and Development - Reference …  (parent page)
    • Information Technology  (parent page of Coding and Development - Reference …)
  • New Menu
  • Authors
  •  
  • Contact Us
  • Sign up
  • Login
    Forgot Password?
  • Follow us on
Image Back to Top Back to top
Language Preference
This website has language specific content. Here you can set the language(s) of your preference.

This setting does not affect the current page. It is used only for filtering pages in menus, preview tiles and search results.

It can be changed any time by using the login menu (if you are logged in) or by clicking the Language button on the left bottom of the page.
Log in to save your preference permanently.



If you do not set this preference, you may see a header like This page has 'language_name' content in page preview tiles.
Search
  • Navigation
  • Similar
  • Author
  • More...
You are here:
All Content / Science and Technology / Information Technology / Coding and Development - Reference … / Windows Shortcut for Getting Location of Selected Item(s)
Table of Contents

Subscribe to Our Newsletter
Follow us by subscribing to our newsletter and navigate to the newly added content on this website directly from your inbox!
Login to subscribe to this page.
Categories  
Tags  
Author  
manisar
Author's Display Image

"Whenever you can, share. You never know who all will be able to see far away standing upon your shoulders!"

I write mainly on topics related to science and technology.

Sometimes, I create tools and animation.


Windows Shortcut for Getting Location of Selected Item(s)

Nov. 27, 2022

Author - manisar


So often we need the location (path) of the files or folders which we have selected in the Windows explorer, e.g. for documentation, or for creating/modifying shortcuts etc.

Generally, in Windows, it's a two step process:

  1. Go to the address bar of the folder and copy the path and paste it somewhere.
  2. Go to 'rename' the item in question, copy the name, and append it to the path pasted above.

I've simplified this task for myself using Python, and given below are the steps to do it.

Using the mechanism described below, we'll add couple of entries to the right-click context-menu, clicking on which will:

  1. Copy the path of the selected file(s) or folder(s) to the clipboard.
  2. Show it in a message box.

We get a context-menu entry like this:

Not only this makes this task faster, using this, we can get paths to multiple files (or folders) in one go.

Let's look at the three steps below.

Create the Python file

import sys
import ctypes
import win32clipboard

MB_OK       = 0x0
ICON_INFO    = 0x40

if __name__ == "__main__":
    paths = '\n'.join(sys.argv[1:])

    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(paths)
    win32clipboard.CloseClipboard()

    MessageBox = ctypes.windll.user32.MessageBoxW
    MessageBox(None, paths, "Paths of Selected Items", MB_OK | ICON_INFO)

Save it with the name Path_to_Selected_File.pyw, and get its full path - we'll need it in the next step.
You may give it any name.
Also, we are using .pyw for silent processing.

Add to Context Menu

Save the following in a .reg file and run it so that these are added to the Windows registry.
Note that we are adding the context menu item for both files and folders.

In the code below, use the Python exe path that exists on your Windows, as well as the full path to the script we saved above.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Get Location (single)]
@=&Get Location (single)

[HKEY_CLASSES_ROOT\*\shell\Get Location (single)\command]
@="\"C:\\Program Files\\Python39\\pythonw.exe\" \"...\\Path_to_Selected_File.pyw\" \"%1\""

[HKEY_CLASSES_ROOT\Directory\shell\Get Location (single)]
@=&Get Location (single)

[HKEY_CLASSES_ROOT\Directory\shell\Get Location (single)\command]
@="\"C:\\Program Files\\Python39\\pythonw.exe\" \"...\\Path_to_Selected_File.pyw\" \"%1\""

Add to "Send To"

The above will handle only single selections.

It, actually, can handle multiple selections - but it will show multiple boxes which is annoying, and only the path of the last item will be in the clipboard in the end.

To handle multiple selections, we can do this:

  1. Go to the sendTo folder by typing shell:sendTo in the explorer address bar.
  2. Place a shortcut to the Python file we saved above at this location.
    Give it an appropriate name.

After this, this action item will be visible in the Send To group in the context menu.

This will allow us to select multiple items and get their paths into the clipboard.

Advertisement

Advertisement
Close ad Ad
Advertisement
Close ad Ad

Return to Coding and Development - Reference and Tools

Tell us what you think (select text for formatting, or click )

Copyright © randompearls.com 2020

Privacy Policy