2

Please image such a situation: A local file's icon is displayed in a GUI, right click the icon, a context menu pops, with the option: show file in explorer. Click the option, then a explorer window opened, with the particular file selected. Many editors have such a feature: show in folder or show in explorer

In fact, the GUI is built by PyQt, my first thought is simple, just open a subprocess and pass the command line:

explorer /select, a_full_path_name

The behavior is indeed what I need, but when click "show in folder" again, a new explorer window will be opened, even the old one exsits! How about a naughty boy clicking "show in folder" dozens of times in a breath? So I need just one window, if an old one exists, just raise it to the front.

The command start /D a_path . may disappoint the naughty boy(run it several times, only one window.) however, there is no option to highlight a selected file, thus also disappoint me...

As mentioned above, many editors have such a "show in folder" feature, but to my suprise, PyCharm "Show in Explorer" will open multiple windows with multiple clicks on the same file, and also the CodeBlocks "opening containing folder", however programmer's notepad "open containing folder" will always open just one folder on the same file.(To be honest, I have only the 3 editors in my PC except the windows notepad :)


My Question:
Can the feature mentioned above be achieved just by windows cmd?
If can not, Is there a python way to achieve that?

In fact, I found several related questions in stackoverflow, for example, but my problem is unsolved, would somebody give me a ride?

Community
  • 1
  • 1
oz1
  • 938
  • 7
  • 18

1 Answers1

2

Finally, some nice guy guided me to the answer.
It's from https://github.com/exaile/exaile/blob/master/xl/common.py#L350

in py3+

import ctypes

ctypes.windll.ole32.CoInitialize(None)
upath = r"C:\Windows"
pidl = ctypes.windll.shell32.ILCreateFromPathW(upath)
ctypes.windll.shell32.SHOpenFolderAndSelectItems(pidl, 0, None, 0)
ctypes.windll.shell32.ILFree(pidl)
ctypes.windll.ole32.CoUninitialize()

in py2+

Just give a unicode path.
note: ILCreateFromPathW (Unicode) and ILCreateFromPathA (ANSI)

oz1
  • 938
  • 7
  • 18