Computer Hope

Microsoft => Microsoft DOS => Topic started by: newage on April 11, 2020, 01:30:02 AM

Title: How can I get the DVD device type into variable
Post by: newage on April 11, 2020, 01:30:02 AM
Hello!
I need to determine whether the DVD device in the computer is DVD Writer or DVD reader
I do it through wmic cdrom where mediatype!='unknown' get mediatype
It returns
MediaType
DVD Writer

However, how can I put "DVD Writer" into variable?

I tried

wmic cdrom where mediatype!='unknown' get mediatype > tmpFile.txt
set /p data=<tmpFile.txt

However the content of %data% is
■M
Title: Re: How can I get the DVD device type into variable
Post by: Sidewinder on April 11, 2020, 10:23:29 AM
Run this at the the console. The variable %mediatype% will have the value DVD Writer

Code: [Select]
for /f "tokens=* skip=2" %i in ('wmic cdrom where "mediatype!=\"unknown\"" get mediatype') do @echo %mediatype%

Good luck.  8)
Title: Re: How can I get the DVD device type into variable
Post by: Sidewinder on April 11, 2020, 11:37:24 AM
Please disregard the previous post. When I returned to check it out, it failed. Sorry for any inconvenience. If I find a fix I will post any updated version.

 8)
Title: Re: How can I get the DVD device type into variable
Post by: Sidewinder on April 11, 2020, 01:44:30 PM
OK, lets try Solution 2.0

The variable %mediatype% will have the value DVD Writer

Code: [Select]
for /f "tokens=* skip=1" %i in ('wmic cdrom where "mediatype!=\"unknown\"" get mediatype') do if not defined mediatype set mediatype=%i

Good luck.  8)


Title: Re: How can I get the DVD device type into variable
Post by: Hackoo on April 11, 2020, 04:36:20 PM
Hi  ;)
This how to fix your problem with a batch file :
The output of WMIC is unicode !
The trailing <CR> can be removed by passing the value through another FOR /F loop.
This also removes the phantom "blank" line (actually a <CR>)
Code: [Select]
@echo off
set "mediatype="
@for /f "skip=1 delims=" %%a in ('wmic cdrom where "mediatype!=\"unknown\"" get mediatype') do (
@for /f "delims=" %%b in ("%%a") do if not defined mediatype set "mediatype=%%~nb"
)

echo MediaType is : "%mediatype%"
pause
The variable %mediatype% will have the value "DVD Writer"