Windows Batch Script Get Current Drive name

前端 未结 8 1780
情话喂你
情话喂你 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:11

    Like others have said %~d0

    There's no point in going over that again, but if you have stumbled into this thread then this will cd to the directory of the batch file.

    @echo off
    set "fullDir=C:\Program Files"
    cd /d %fullDir%                                &rem changes to the full directory 'C:\Program Files'.
    echo You're now cd' in the '%cd%' directory.
    pause
    

    Also if you want to run a batch file from another drive such as Z: then this one will do that.

    cd /d %~d0                                     &rem Changes to the current directory
    echo You're now cd' in the '%cd%' directory.   &rem This is now your full path of the batch file cd' into.
    pause
    

提交回复
热议问题