Reputation: 192
First of all, I have almost no idea about batch language. I'm working on a batch file that writes to the registry in order to add a context menu option that removes "desktop.ini" from the folder from where the context menu option is called. So far, what I got is:
@echo off
@reg add "HKEY_CLASSES_ROOT\Folder\shell\resetFolderSettings" /t REG_SZ /v "" /d "Reset folder settings" /f
rem @reg add "HKEY_CLASSES_ROOT\Folder\shell\resetFolderSettings\command" /t REG_SZ /v "" /d "del /A s h \"%cd%\desktop.ini\" && pause" /f
@reg add "HKEY_CLASSES_ROOT\Folder\shell\resetFolderSettings\command" /t REG_SZ /v "" /d "cmd.exe /c echo \"%~dp0\desktop.ini\" && pause" /f
pause
Which doesn't work because what gets written into the registry is the static folder path from where the installation .bat is called. I'm having quite some trouble finding a solution to this problem so I finally decided I had no choice but to ask for help here.
Upvotes: 0
Views: 270
Reputation: 30113
Use %V
to denote the folder from where the context menu option is called.
Hence, the parameters you should use in
reg add "HKCR\Folder\shell\resetFolderSettings\command" ...
could be from a batch script (see that %V
should be escaped as %%V
):
... /t REG_SZ /v "" /d "%comspec% /c echo \"%%V\desktop.ini\"&&pause" /F
or directly from cmd
window - %
sign is not escaped here:
... /t REG_SZ /v "" /d "%comspec% /c echo \"%V\desktop.ini\"&&pause" /F
Edit. To make %comspec%
expandable as well (and with operational del /A
command instead of echo
):
... /t REG_EXPAND_SZ /ve /d ^%comspec^%" /c del /A \"%V\desktop.ini\"&pause" /F
used directly from cmd
window. See escaped %
as ^%
(however only those out of double-quoted part of command line) above.
Use another escaping scheme from a batch-script: escape all %
as %%
how seen in both %%comspec%%
and %%V
as follows:
... /t REG_EXPAND_SZ /ve /d "%%comspec%% /c del /A \"%%V\desktop.ini\"&pause" /F
Result:
==> reg query "HKCR\Folder\shell\resetFolderSettings\command" /ve
HKEY_CLASSES_ROOT\Folder\shell\resetFolderSettings\command
(Default) REG_EXPAND_SZ %comspec% /c del /A "%V\desktop.ini"&pause
Upvotes: 2