0

While cmd.exe can work with UNC paths by using PUSHD \\UNC\Path, I'm attempting to start cmd or a similar command FROM a UNC path. Here's why:

I'm adding a "Map Network Drive Here" to my suite of context menu tools, like "Start Powershell Here" and "Open Command Prompt Here". They add registry keys to the windows registry, and this works great.

As examples, these open a Command Prompt Here:

"cmd /s /k ""VER && TITLE Command Prompt: %1 && PUSHD ""%1"" "" " (For Right-Clicking Folders, works for UNC paths)

"cmd /s /k ""VER && TITLE Command Prompt: %%%CD%%%"" " (For Right-Clicking backgrounds of directories, does not work for UNC paths)

The command is entered as a registry key DWORD, and this works when right clicking a folder:

"net use * ""%1"" " (For Right-Clicking Folders, works for UNC paths)

"net use * ""%%%CD%%%"" " (For Right-Clicking backgrounds of directories, does not work for UNC paths)

I suspect %%%CD%%% is being expanded by cmd.exe, which will not start in a UNC path.

My question is; how do I get the path of the right-clicked 'background of a drive or directory' for both UNC and Windows paths expanded in a registry key for the purposes of passing it to a command?

These will go in HKCR,Directory\Shell\xxx\command and HKCR,Directory\Background\Shell\xxx\command, respectively.

This is an open source project, available on GitHub: https://github.com/Ehryk/ContextMenuTools

1 Answer 1

1

I'm not sure... but did you mean something like this? (you need to use %V instead of %1)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmd]
@="Open cmd here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmd\command]
@="cmd /s /k \"VER && TITLE Command Prompt: \"%V\"  && PUSHD \"%V\" \""

(B.T.W. that NoWorkingDirectory is for that you don't get an initial error message when cmd is opened with a UNC as working directory. This starts cmd without a current directory after which you do a pushd to get a temporary drive-letter)

Edit: Because you're clicking in a blank space of a directory the %1 parameter is not filled in (like it is if you click on the directory itself). So you need to use %V or %W. These stand for the working directory which is the UNC path when right-clicking in a blank space of a networked directory.

See here for a complete list of these variables.

4
  • This works for 'Command Prompt Here' as it maps the drive and opens the command window. What's the difference between %1 and %V?
    – Ehryk
    Commented Jul 25, 2014 at 21:49
  • @Ehryk %1 doesn't work in Directory\Background\Shell ;) Because you clicked a blank background there is no parameter for %1. %V is the working directory which is available for a right-click in the blank background of a directory (still the UNC when called by command). You can see a list of these special variables here. %W works too.
    – Rik
    Commented Jul 25, 2014 at 21:58
  • I added the info to my answer.
    – Rik
    Commented Jul 25, 2014 at 22:04
  • %V was exactly what I needed, and that variable list helped immensely. Thanks!
    – Ehryk
    Commented Jul 26, 2014 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .