user1615760
user1615760

Reputation: 21

batch that return drive letter of USB with specific volume name

I do not have much experience with batch an d need a help with batch script. Task is, return drive letter as parameter to %disk_letter%

Idea is use this for search:

WMIC LogicalDisk Where VolumeName='MY_USB' Get /Format:list | FIND "Caption="

I have "Caption=G:" as the result. I need that %disk_leter% parameter was equal just "G:" Need help to finish this script.

Thank you!

Upvotes: 0

Views: 2596

Answers (2)

sparky3489
sparky3489

Reputation: 99

Here goes...

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

Upvotes: 0

chucksmash
chucksmash

Reputation: 5997

On Linux right now but here's what I think you'll need to do. Part 1: save the result of your FIND command to a variable, and 2: take a substring of the variable. The second part is simple, so I'll start with that (assuming that in the first step you named your variable var

@echo %var:~-2%

That's about as far as I'm comfortable in batch, so this next bit is cobbled together:

To store the result of your find as a variable, try amending your code to:

set cmd="WMIC LogicalDisk Where VolumeName='MY_USB' Get /Format:list | FIND "Caption=" "

FOR /F %%i IN (' %cmd% ') DO SET var=%%i

and then (remember above) output it with:

@echo %var:~-2%

The related question from which I am cobbling together the second part is this question so if this doesn't work as expected I would jump over to that one first.

Upvotes: 1

Related Questions