Jefferson
Jefferson

Reputation: 101

How to write the letter of a removable device in a variable using the CMD?

I am writing a program in .bat. Suppose the command prompt is at D:\>. I need to write the letter D in a variable. It's possible?

Upvotes: 1

Views: 134

Answers (2)

ScriptKidd
ScriptKidd

Reputation: 851

To save the current path as a variable (much easier: %__CD__:~,1%):

FOR %%A in ("%__CD__%") do set "disk=%%~dA"
set "disk=%disk:~0,1%"

Edit: Alternatively,

FOR /F %%D in ('"prompt $N&cmd/k<nul"') do set "disk=%%D"

Sources:

Upvotes: 3

Io-oI
Io-oI

Reputation: 2565

You may not need an additional for loop to define the driver, because you have a system variable %cd% or %__CD__% for this task, use it through the substring variable

%CD:~0,1%

rem :: or...
%__CD__:~0,1%

@echo/%CD:~0,1%

rem :: or...
@echo/%__CD__:~0,1%

@set "drive=%CD:~0,1%"

rem :: or...
@set "drive=%CD:~0,1%

@set "drive=%__CD__:~0,1%"

Obs.: Also works %CD:~,1%, %__CD__:~,1%

String Manipulation in bat/cmd file

Upvotes: 1

Related Questions