开发者

Windows Batch Script Get Current Drive name

I have a batch file which is on a usb key. I need to know the driv开发者_开发问答e name the batch is in.

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

How could I do that in batch script. (Windows)


%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.


M$ documentation "Using batch parameters" says:

Modifier: %~d0

Description: Expands %0 to a drive letter.


Beware

The existing answers to this question don't acknowledge that the question is actually asking about two different things:

  1. the drive of the current working directory (“Get Current Drive name”); and
  2. the drive of the batch file (“I need to know the drive name the batch is in”).

These can be different when the batch file is started from a working directory other than its residing location. Readers should therefore be clear on the difference before determining which solution is relevant to their case.

Drive of the current working directory

%CD:~0,2%

This takes the full current working directory and trims it down to the first two characters, which will be a drive letter and a colon, e.g. C:.

Drive of the batch file itself

%~d0

This trims the full path of the batch file (in %0) to just a drive letter and a colon, e.g. C:.


(this is an expanded version of my rejected edit to Kai K's answer)


If run from inside a .CMD/.BAT file, you can use %~dp0 to get the current/working directory. This one is a little safer as it is aware of UNC paths and such. Reference for the syntax of that variable is available here.


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.
  )
)


Thanks Very Much @Sparky3489, if I have just one USB Flash Drive, I put this within your Algorithm, Right After the

echo %%i is a USB drive.
Set FlashDrive=%%I

I also changed the Wording of the Identifier to

Echo %%i is a USB Flash Drive~!

Then, After, {and Outside} the Algorithm, I can add the Flash Drive Path such as...

Set FlashPath=%FlashDrive%\Users\Public\Documents

Then by setting up other Paths Such as

Set SourcePath=C:\Users\Public\Documents

I can make an Batch File BackUp For the Flash Drive, (can be Called Via Windows Short-Cut with an Associated Icon in your Quick Launch Window ~ Search "Quick Launch", if in Doubt to what I'm Talking About).

Rem  * * * * * * * * * Start Batch File * * * * * * * * * *
@Echo OFF
cls
Echo FlashDrive UpDater for
Echo.
Echo Excel, Word ...
Echo * * * * * * * * * ~ Excel SpreadSheets ~ * * * * * * * * *
XCopy /D /I /V /Y /U /S "%SourcePath%\Excel Documents\*.*" "%FlashPath%\Excel Documents\"
Echo * * * * * * * * * ~ Word Documents ~ * * * * * * * * *
XCopy /D /I /V /Y /U /S "%SourcePath%\Word Documents\*.*" "%FlashPath%\Word Documents\"
Echo.
Echo.
Echo FlashDrive = %FlashDrive%
Echo FlashPath = %FlashPath%
Echo.
Echo * Bonus Switch Info * * * * *
Echo * XCopy Switch  /D ~ Copies Files Changed On or After the Specified Date.
Echo *          {If no Date is Given, Copies only those Files whose
Echo *          Source Time is Newer than the Destination Time}.
Echo * XCopy Switch  /I ~ Copies More than One File to Destination (Assumes Destination is a Directory)
Echo * XCopy Switch  /S ~ Copies Directories and Subdirectories Except Empty Ones
Echo * XCopy Switch  /V ~ Verifies Each New File.
Echo * XCopy Switch  /U ~ Copies only Files that Already Exist in Destination.
Echo * XCopy Switch  /Y ~ Suppresses Prompting to Confirm You Want to Overwrite an Existing Destination File.
Echo.
Rem for More Info on XCopy Switches GoTo http://support.microsoft.com/kb/128756
Echo Directory Path = %~DP0
Echo.
Echo * Batch File Name = %0 *
Echo.
Rem Echo %CD:~0,2%, {Returns "Drive Letter & Colon"}
Rem Echo %CD:~0,3%, {Returns "Drive Letter & Colon & BackSlash"}
Pause
cls
Pause
Exit
Rem  * * * * * * * * * End Batch File * * * * * * * * * *


%CD:~0,2%

This will give you the current drive in the format C: , i.e. first 2 chars from the current working directory

C:\Users\ashish>ECHO %CD:~0,2%
C:

D:\projects>ECHO %CD:~0,2%
D:

D:\projects>ECHO %CD%
D:\projects


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜