Find USB Drive letter

前端 未结 5 455
星月不相逢
星月不相逢 2020-12-18 16:57

I need to get the drive letter of the usb pen drive.the command CHDIR >drive.txt gives me the drive letter L:.How do I read/get this info \"L:\" without quotes into a variab

5条回答
  •  太阳男子
    2020-12-18 17:26

    Whatever chdir outputs can also be used by the pseudo-variable %CD%. So you already have a variable with that information.

    Otherwise you can use for:

    for /f "delims=" %%x in ('chdir') do set "drive=%%x"
    

    But I think %CD% is much easier :-)

    EDIT: You said that with the quotes already. You won't have any quotes in the variable unless you want them in there.

    As for the trailing backslash: You can use the following instead of %CD%:

    %CD:~0,-1%
    

    which will expand %CD% and remove the last character, which you know is the backslash.

    Still, will only work if you're in the root directory of that drive. Otherwise you can also use

    for %%x in (%cd%) do @set drive=%%~dx
    

提交回复
热议问题