Ikrananka
Ikrananka

Reputation: 35

How to check for specific disc and pass on drive letter to another batch file?

I would like to use a batch file to check that a specific disc is in a drive by reference to its volume name, and to then pass the drive letter that the disc is in to another batch file. I managed to find something that almost works for me, but not quite: Get the drive letter of a given volume?. I have been attempting to get the following to work but the syntax is now incorrect and I don't know where.

SET "volname=Desired_Name"
FOR %%d IN (Z Y X W V U T S R Q P O N M L K J I H G F E D C B A) DO IF EXIST %%d:\. (
 FOR /f "tokens=5*" %%L IN ('VOL %%d:^|FIND /i "drive"'
  ) DO IF "%%L"=="is" IF "%%M"=="%volname%" set drive=%%d & goto success
)
goto :eof
:success
success.bat %drive%

Upvotes: 0

Views: 71

Answers (2)

Ikrananka
Ikrananka

Reputation: 35

Thanks Magoo, your answer above works perfectly for me. I also just came up with a slightly simpler alternative based on the answer to this question Refer to/select a drive based only on its label? (i.e., not the drive letter).

@echo off
for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "HTML4_WCC"') do set 
drive=%%D
if not "%drive%"=="" goto success
goto :eof
:success
echo Disc in %drive%
goto :eof

Upvotes: 0

Magoo
Magoo

Reputation: 80193

I'd count your parentheses if I were you - one more open than closed...


Ugh! Now I've cleaned my monitor...

I have 2 DVD drives and found that if I'd put the target in one, tested and moved it to the other, the empty drive gave a nasty pop-up.

So I fiddled with the code and came up with this, which seems immune to missing content:

@ECHO OFF
SETLOCAL
SET "volname=HTML4_WCC"
FOR %%d IN (Z Y X W V U T S R Q P O N M L K J I H G F E D C B A) DO (
 FOR /f "tokens=5*" %%L IN ('VOL %%d: 2^>nul^|FIND /i "drive"'
  ) DO IF "%%L"=="is" IF "%%M"=="%volname%" set drive=%%d & goto success
)
goto :eof
:success
ECHO Disc in %drive%
GOTO :EOF

Or possibly you have a case-mismatch and require if /i ?

Upvotes: 1

Related Questions