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


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

@="Open cmd here"

@="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.

  • 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 Jul 25 '14 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 Jul 25 '14 at 21:58
  • I added the info to my answer. – Rik Jul 25 '14 at 22:04
  • %V was exactly what I needed, and that variable list helped immensely. Thanks! – Ehryk Jul 26 '14 at 5:46

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.