Windows Batch Script Get Current Drive name

前端 未结 8 1791
情话喂你
情话喂你 2021-02-02 09:30

I have a batch file which is on a usb key. I need to know the drive name the batch is in.

Example, if it\'s E:\\mybatch.bat it should find E:\\ same thing for F:\\, G:\\

相关标签:
8条回答
  • 2021-02-02 10:16

    You can find all USB drive letters from any drive with this.

    @echo off
    
    for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
    
    if %%l equ 2 (
    echo %%i is a USB drive.
            )
            )
    
    0 讨论(0)
  • 2021-02-02 10:20

    %CD% is what you're looking for. It prints the current working directory of the batch file or command running it. If your batch file is on the root of the drive, it will just print the drive letter, otherwise you'll have to parse the first 2 characters.

    Example:

    echo %CD%
    

    prints

    E:\
    

    on a flash drive mounted to E:.

    Update: As Andriy said in the comments, if you are just looking for the first three characters of the path, then use this instead of %CD%:

    %CD:~0,3%
    

    This will result in E:\, for example, anywhere on the drive.

    0 讨论(0)
提交回复
热议问题