简体   繁体   中英

Remove Trailing \ in subst Command

I have added a command to my Context Menu via the Registry in HKCR\\Drive\\shell\\MapLocalDriveHere\\command such that when I right click a drive. I'd like it to give me the name of the drive that I have right-clicked as "C:" not "C:\\", as this causes problems with the command I'm trying to run.

cmd /c subst %1 /D

This expands to:

cmd /c subst C:\ /D

And the command fails (it expects subst C: /D ). How do I get the parameter without the trailing \\, or remove it? %~d1 and %~1 don't expand from the registry key, type REG_EXPAND_SZ.

You can understand better what I'm trying to do by viewing the source of the project, located at https://github.com/Ehryk/ContextMenuTools (especially this file: https://github.com/Ehryk/ContextMenuTools/blob/master/Custom%20Installs/Map%20Local%20Drive%20Here/MapLocalDriveHere.inf )

你进入一个带有%%~d参数的for /f循环带冒号的驱动器号:

FOR /f "delims=" %%a IN ("%~1") DO cmd /c subst %%~da /D

Well... Let's combine the best features of captcha's and npocmaka's answers. There is no need for an intermediate variable, nor is there a need for a FOR statement:

cmd /c subst %~d1 /D
set "param=%~1"
set "param=%param:~0,-1%"
cmd /c subst %param% /D

For some reason, I had to switch to %V instead of %1 to resolve this in the registry:

cmd /c mapdrive.bat ""%V"" /D

HKCR,Drive\Shell\MapLocalDriveHere\command,,0x00020000,"cmd /c mapdrive.bat ""%V"" /D"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM