Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 )

like image 413
Ehryk Avatar asked Jul 19 '26 10:07

Ehryk


2 Answers

You get in a for /f loop with the %%~d parameter the drive letter with a colon:

FOR /f "delims=" %%a IN ("%~1") DO cmd /c subst %%~da /D
like image 154
captcha Avatar answered Jul 21 '26 00:07

captcha


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
like image 32
dbenham Avatar answered Jul 21 '26 00:07

dbenham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!