Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test the current drive letter in a batch file?

I'm trying to write a batch file that takes the drive letter the batch file is being run from, and uses it an an IF statement. If the letter is M: for example, it will jump to the label :mSection.

Is this even possible?

like image 992
JimDel Avatar asked May 15 '09 01:05

JimDel


People also ask

How do I read a Windows batch file?

To open the BAT file in Notepad, right-click it and choose Show more options > Edit from the menu (or just Edit in some Windows versions). You might find it helpful to use more advanced text editors that support syntax highlighting when editing a BAT file.

What is D in batch file?

/d -- This switch makes cd change both drive and directory at once. Without it you would have to do cd %~d0 & cd %~p0 . ( %~d0 Changs active drive, cd %~p0 change the directory). %~dp0 -- This can be dissected further into three parts: %0 -- This represents zeroth parameter of your batch script.

What does for F mean in batch?

FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.


2 Answers

You can use %~d0 to get the drive letter. Something like this:

IF "%~d0"=="M:" CALL :mSection
like image 186
Dave Cluderay Avatar answered Nov 13 '22 15:11

Dave Cluderay


You can use %~d0 to get the drive letter...

It doesn't work whet the .bat is on different drive (somewhere in the PATH.)

I'd suggest:

echo %cd:0,2%
like image 38
user10786435 Avatar answered Nov 13 '22 13:11

user10786435