71

When extending the Windows' shell context menu (e.g. for adding an 'Open command here' prompt on directories), a 'command' key needs to be created in the registry.

The value of this 'command' key apparently can be any valid command line.

I want to know which 'special variables' are available for use inside this command line.

For example, I use following command for opening a cmd window from within a directory's context menu (*):

cmd.exe /e:on /f:on /s /k pushd "%V"

I cannot find any reference to what %V actually means or what the full list of such variables is.


(*) Following registry keys are created for this:

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\cmdshell]
@=Open Command Prompt Here"

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\cmdshell\command]
@="cmd.exe /e:on /f:on /s /k pushd \"%V\""

4 Answers 4

83

A comment by Chris Guzak on the Extending Shortcut Menus MSDN article lists the various "command line variables" that are available:

%* – Replace with all parameters.

%~ – Replace with all parameters starting with and following the second parameter.

%0 or %1 – The first file parameter. For example "C:\Users\Eric\Desktop\New Text Document.txt". Generally this should be in quotes and the applications command line parsing should accept quotes to disambiguate files with spaces in the name and different command line parameters (this is a security best practice and I believe mentioned in MSDN).

%<n> (where <n> is 2-9) – Replace with the nth parameter.

%s – Show command.

%h – Hotkey value.

%i – IDList stored in a shared memory handle is passed here.

%l – Long file name form of the first parameter. Note that Win32/64 applications will be passed the long file name, whereas Win16 applications get the short file name. Specifying %l is preferred as it avoids the need to probe for the application type.

%d – Desktop absolute parsing name of the first parameter (for items that don't have file system paths).

%v – For verbs that are none implies all. If there is no parameter passed this is the working directory.

%w – The working directory.

So %L or %l should be preferred.

Also see http://www.robvanderwoude.com/ntstart.php

4
  • 23
    I don't understand what is meant by "For verbs that are none implies all". Dec 17, 2012 at 21:16
  • 3
    A warning about %W: It is not always available and will throw a cryptic error message if used in your command value. For example, calling your context menu item on a drive's or a library folder's context menu will not initialize this variable. Avoid its use outside of a file handler's context menu entry. Nov 5, 2017 at 7:17
  • Has anybody tried %~? I only get an error.
    – cdlvcdlv
    Jan 5, 2018 at 22:28
  • 4
    %V should be used if you want directory name, ie. when you want to add your command on context menu when you click on background, not on a single file or a directory name. %L won't work in that case. May 1, 2018 at 6:26
18

This question intrigued me so I did some experimenting. I have a folder C:\iso. What I found when I did the right-click thing:

D = C:\iso
H = 0
I = :115057472:7932
L = C:\iso
S = 1
V = C:\iso
W = C:\

I returned a different set of numbers on every try. H was always 0 and S was always 1. D, L, and V were all the target folder. W was the parent of the target folder. Anyone care to expand on this?

FYI: I used the following registry entries to test:

[HKEY_CLASSES_ROOT\Directory\shell\testcmd]
@="Test Command Window Directory"
[HKEY_CLASSES_ROOT\Directory\shell\testcmd\command]
@="cmd.exe /k \"echo %A`%B`%C`%D`%E`%F`%G`%H`%I`%J`%K`%L`%M`%N`%O`%P`%Q`%R`%S`%T`%U`%V`%W`%X`%Y`%Z\""

(The ` characters were used for delimiters)

1
  • 7
    Did you right click in the folder, or on the folder? Dec 17, 2012 at 21:16
3

Here is the arguments/syntax for cmd.exe

/k carries out the command specified by the following string so it executes the command pushd %V and since the only argument pushd accepts is a path it follows that %V a variable delivered by explorer that contains the path of the folder right clicked.

3

It is indeed hard to find what %V means or a list of those variables, through Google I have found that there seems to exist a %L too. I don't think you have to pass more than %V or %L though to a prompt, as there is no other useful information I think. Where %V could be the directory name, %L could be a location to a file. Doing an 'echo' could help when in doubt...

You must log in to answer this question.

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