5

I'd like to open and select a file in an existing Explorer window from the command line.

explorer.exe /select, "FILE_PATH"

Opens a new window.

cmd.exe /c start "" "FILE_PATH"

Runs the file.

Is it possible to use

cmd.exe /c start "" "FOLDER_PATH"

and then select a specific file?

Thank you.

Yaron
  • 133
  • 2
  • 13
  • 1
    I'm using [QTTabBar](http://qttabbar.wikidot.com/) and I'd like to open a file's containing folder in a new tab in the existing window. Thank you. – Yaron Jul 06 '17 at 02:07
  • The issue is not only relevant to QTTabBar. In general: can you open a folder in an existing window and select a file? Thanks again. – Yaron Jul 06 '17 at 19:38
  • Indeed. I want to accomplish the following: **1)** Open up a specific folder. **2)** Open up a specific file within that same folder. **3)** DO NOT open a new Explorer window but rather navigate in the existing one (or open a new tab if you use QTTabBar). *** I'd like to use the command in Notepad++: Open the containing folder of the active file and also select it. So it's not limited to a certain file extension. *** Thank you. I appreciate your help. – Yaron Jul 07 '17 at 00:29
  • Great. I appreciate that. – Yaron Jul 07 '17 at 01:26
  • Yaron - Check out and test with the batch script here: http://textuploader.com/dkl5q to start with and confirm if that works as expected for the first parts of your request and let me know. If I'm on the right track I will move forward with more testing as soon as I get a chance with the QTTabBar software. This will help me determine if you need adjustments, if something is way off from your expectations, etc. Obviously you'll need to change the value of the file and the folder variables to match those you will test with. – Vomit IT - Chunky Mess Style Jul 07 '17 at 14:00
  • Thank you very much! I appreciate your kindness.*** I've slightly modified your script and it works perfectly. https://s9.postimg.org/a2d9opy3z/Bat_Script.png *** I couldn't figure out yet how to pass the FolderPath and FileName as arguments. – Yaron Jul 07 '17 at 16:37
  • I've figured out how to use the arguments: https://s14.postimg.org/s4mq9txj5/Script.png. *** There's one problem: if the file name includes certain characters (e.g. {07E520A6-15F9-4D0F-A577-74945ABDC1DE}.reg) or the characters are not Latin (but Hebrew in my case) it won't get selected.*** Thank you so much. I'm really grateful. *** Any up-voting/appreciation mechanism on this site? – Yaron Jul 07 '17 at 20:57
  • With your permission, another question: the BAT file is "used" for a while after executing it; that is: it's "Read Only". Any idea why? *** BTW, I'm using NirCmd in order to hide the console. `nircmd.exe exec hide "Bat File" "Folder Path" "File Name"`. *** Many thanks again. – Yaron Jul 07 '17 at 21:05
  • For the Hebrew deal check out https://stackoverflow.com/questions/30478940/how-to-fix-a-batch-file-with-an-hebrew-font and how to use chcp to help with that. I could not get this recreated for the file with the name of `{07E520A6-15F9-4D0F-A577-74945ABDC1DE}.txt` when I tested with it, it seems to open fine on my system but may be related to your locale and the chcp tips from there may help resolve. There's likely another process that's making the batch file read only like setting an `attrib +r` with another process. – Vomit IT - Chunky Mess Style Jul 08 '17 at 00:33
  • I've encoded the BAT file to Windows 1255 (Hebrew). The problem is with the VBS file which is encoded to UTF 16-LE. How can I change that encoding from the BAT file? Alternatively, I could create and keep a VBS file with the correct encoding; but then the `ECHO Set p = CreateObject("WScript.Shell") >> "%TempVBSFile%"` part would have to overwrite the previous entries. *** As I'm using QTTabBar, I need to use `start "" %1` instead of `explorer` (which opens a new window); for some reason on every third-or-so use of the batch script (from Notepad++), the Explorer window remains in the background – Yaron Jul 08 '17 at 02:31
  • ... But that is a minor issue. *** Allow me to thank you again for your kind help. Highly appreciated. *** I've up-voted some of your answers. I'll certainly remember to up-vote your answer in this thread. :) *** Have a nice weekend and enjoy your time with the family. – Yaron Jul 08 '17 at 02:39
  • 1
    `SendKeys()` does not support Unicode. See https://superuser.com/questions/1230810/vbs-select-one-file-in-a-folder for an alternative. @McDonald's, Thanks again. – Yaron Jul 18 '17 at 20:35
  • https://superuser.com/questions/1233283/vbs-select-item-in-windows-explorer-last-active-window – Yaron Jul 22 '17 at 05:18
  • @Yaron vbscript is old and so is the sendkeys accessible from it, but the sendkeys in .net e.g. in C# or VB, does support unicode. so e.g. VB's SendKeys.Send does. – barlop Jul 24 '17 at 14:41
  • You have a command to open a folder and select a file, and you have a command to run a file, why don't you just use both commands? – barlop Jul 24 '17 at 14:44
  • Thanks for your help. *** I might try the other script languages. *** I don't want to run the file. Just select it. – Yaron Jul 24 '17 at 14:50
  • 1
    Here is an example of vbscript with sendkeys https://stackoverflow.com/questions/3198574/does-or-can-vbscripts-sendkeys-support-unicode Note, if you used C#, note that C# is a proper programming language, not a scripting language – barlop Jul 24 '17 at 14:56
  • Thank you very much. *** I later realized that sending keys is not enough as I need to activate first a specific Windows-Explorer window. *** Sorry for the confusion by the questions I posted in different stages. – Yaron Jul 24 '17 at 15:08

1 Answers1

2

CMD is an interpreter; gives users an interface/language to access features of Windows™. It does NOT give direct access to system memory or CPU registers (a feature of lower level programming language).
To "open and select a file" (in explorer window) you would:

explorer.exe /select, "FILE_PATH"

To just open a explorer window to select a file manually, you would:

cmd.exe /c start "" "FOLDER_PATH"

If "an existing Explorer window" is already open, and you want to just select a file manually, you would Activate the explorer window (that is already open instead of opening another explorer window).

Windows manipulation is a feature of GUI and CMD provides a CLI to Windows™, hence you'd need to write code in a (lower level) language or one that gives you access to such features ie. to activate open windows eg. other interpreters (AutoHotKey/AutoIt/VBS), compilers (C/VBA), or assembly/machine code (which can be interfaced via CMD (forfiles or fc) or related utilities eg. Debug or Certutil)
See:
CMD command in Windows to switch to an already open application

A workaround would be to open your chosen folder via explorer "FOLDER_PATH" for manual selection of file or autoselected via explorer.exe /select, "FILE_PATH" & close the existing explorer window via taskkill /im explorer.exe /fi "windowtitle eq %title%"

Zimba
  • 1,051
  • 11
  • 15