China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-08 20:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Seeking Improvement] A New Method to Judge the CD-ROM Drive View 2,799 Replies 17
Original Poster Posted 2008-05-26 19:04 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
The problem arises like this.

I saw someone wrote an example of judging the CD - ROM:

for %%i in (C: D: E: F: G: H: I: J: K: L: M: N: O: P: Q: R: S: T: U: V: W: X: Y: Z:) do if exist %%i\WIN51 set CDROM=%%i

The above method is very simple, but there is a problem. That is, when there is no CD - ROM in the optical drive, an error message will pop up, and then the subsequent commands cannot be executed. What's worse, if there are two optical drives and the CD - ROM is in the second optical drive, the result cannot be obtained at all because the first optical drive cannot be read and the second optical drive cannot be detected.

So I used the Dready small program. The usage of this small program is Dready /w, which returns errorlevel 0, 1, 2, 255. If it returns 1, it means it is Readonly, that is, it means this is an optical drive and it is ready.

I modified the program:

for %%i in (C: D: E: F: G: H: I: J: K: L: M: N: O: P: Q: R: S: T: U: V: W: X: Y: Z:) do Dready %%i &if errorlevel 1 echo %%i>>cdrom.txt

My idea is to first detect the readable optical drives into a temporary file, and then use the For command again to detect the file WIN51 in the optical drive. The second step is easy to compile. The problem is in the first step above.

The result of the cdrom.txt file is not what I expected because the value of errorlevel has always been the value of the last detected optical drive. I think this is the problem of delayed variables.

So I added setlocal ENABLEDELAYEDEXPANSION in the program and used!errorlevel! instead of %errorlevel%, but I actually found that!errorlevel! is not a value but!errorlevel! itself.

Finally, I simply compiled a program as follows:

setlocal ENABLEDELAYEDEXPANSION
set test="No problem"
echo !test!

The actual running result is not No problem, but!test!, what's the matter?

When I copied the example, it was correct. When I compiled it myself, it was wrong, and it's not the problem of full - width and half - width. I still have this knowledge.

In order to avoid mistakes, many sentences are copied. I just wrote set = xxx myself, but it's wrong. I can't figure it out.

Please help me, thank you!

[ Last edited by quya on 2008 - 5 - 29 at 12:41 PM ]
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 2 Posted 2008-05-26 19:10 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Please provide the Chinese text content that needs to be translated so that I can perform the translation as required.
Floor 3 Posted 2008-05-26 19:19 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
See if this suits you.

@echo off
set "var1=fsutil fsinfo drives"
set "var2=fsutil fsinfo drivetype"
for /f "skip=2 tokens=1 delims=:" %%a in ('%var1%^|more') do (
for /f "delims=" %%i in ('%var2% %%a: ^| find/i "cd-rom"') do call :loop "%%i"
)
pause&exit
:loop
set num=%~1
set num=%num:~0,1%
if exist %num%:\nul.ext (
echo.&echo The CD-ROM is drive %num%, there is a CD inside
) else (
echo.&echo The CD-ROM is drive %num%, no CD
)
goto :eof


[ Last edited by 26933062 on 2008-5-26 at 07:33 PM ]
致精致简!
Floor 4 Posted 2008-05-26 20:07 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Post #3, your code is really amazing. I don't understand it, but the functionality is exactly what I need.

Your code is quite complex, and I have no way to start learning it.

It's estimated that even if you are patient, I may not understand it if you explain it.

I just want to humbly ask you, how should I code according to my train of thought? Because I know my code.

Thank you!!!
Floor 5 Posted 2008-05-26 20:17 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Haha, I kind of understand your code because fsutil is actually a command.

And fsutil fsinfo drivetype [drives] will display things like "CD-ROM", "fixed drive" and so on.

Thank you so much, I learned a trick!
Floor 6 Posted 2008-05-26 20:37 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
It still doesn't work. I put a disc in the first CD drive and the second CD drive has no disc. The information is successfully displayed.

The second time it's reversed, the second CD drive has the disc and the first CD drive has no disc, then a pop-up message appears.

Then it returns to the first situation again, still popping up.

Repeated N times, and it hasn't succeeded anymore.

Puzzled.......
Floor 7 Posted 2008-05-26 21:22 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off
for /f "delims=驱动器:\ " %%i in ('fsutil fsinfo drives^|more') do fsutil fsinfo drivetype %%i: |findstr "CD-ROM">nul&&call :lp %%i
pause>nul&goto :eof
:lp
if exist %1:\autorun.inf (
echo There is a disc in drive %1.
) else(
echo There is no disc in drive %1.
)
```

[ Last edited by zw19750516 on 2008-5-26 at 10:14 PM ]
批处理之家新域名:www.bathome.net
Floor 8 Posted 2008-05-26 22:12 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
:How about this


@echo off
set "var1=fsutil fsinfo drives"
set "var2=fsutil fsinfo drivetype"
for /f "skip=2 tokens=1 delims=:" %%a in ('%var1%^|more') do (
for /f "delims=" %%i in ('%var2% %%a: ^| find/i "cd-rom"') do call :loop "%%i"
)
echo.&pause&exit
:loop
set num=%~1&set "str="
set "num=%num:~0,1%"
dir %num%: >nul 2>nul
if %ERRORLEVEL% equ 0 (echo.&echo The CD is drive %num%, there is a CD inside) else (
echo.&echo The CD is drive %num%, no CD
)
goto :eof


[ Last edited by 26933062 on 2008-5-26 at 10:16 PM ]
致精致简!
Floor 9 Posted 2008-05-26 22:27 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
There is an error in the program on floor 7. The one on floor 8 is almost perfect, but there is a small mistake. The running results are as follows:

The CD - ROM is drive H, no CD - ROM.

The CD - ROM is drive I, there is a CD - ROM inside.

Press any key to continue...

The CD - ROM is drive ~0,1, no CD - ROM.

It would be better to remove that last invalid message. I am also researching where this small mistake occurs.

Thanks again!

The idea of floor 8 is ingenious. Using dir on a CD - ROM without a CD - ROM will only display "Device not ready" (no error message will be popped up), which is much better than that judgment sentence of if exist file!!! Oh! Why do everyone only think of the latter?

[ Last edited by quya on 2008 - 5 - 26 at 10:32 PM ]
Floor 10 Posted 2008-05-26 22:33 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Is this error message appearing after "Press any key to continue..."? If yes, are you sure you copied the code from building 8 completely? Because there is exit later!!~~~
致精致简!
Floor 11 Posted 2008-05-26 22:43 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
I've sorted it out

@echo off&setlocal EnableDelayedExpansion
set "var1=fsutil fsinfo drives"
set "var2=fsutil fsinfo drivetype"
for /f "skip=2 tokens=1 delims=:" %%a in ('%var1%^|more') do (
%var2% %%a: | find/i "cd-rom">nul&&(
set "str=%%a"
set "str=!str:~0,1!"
dir !str!: >nul 2>nul
if !ERRORLEVEL! equ 0 (echo.&echo The CD-ROM is drive !str! and there is a CD in it) else (
echo.&echo The CD-ROM is drive !str! and there is no CD
)))
echo.&pause&exit
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
WANKOILZ +1 2008-05-29 19:33
致精致简!
Floor 12 Posted 2008-05-26 22:57 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
I need to debug, and it will be used in batch processing later, and also need to assign values, so I can't EXIT. What does "2>nul" mean in "dir >nul 2>nul"? Without writing this, there is information echoing. I also tried "3>nul", and it seemed to enter a shell, so I had to EXIT and quit. Oh! I've never seen such a usage before. "26933062" is an expert, I have to admire.
Floor 13 Posted 2008-05-26 22:59 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Instead of using exit, change it to goto target label. Otherwise, after displaying, it will continue to run: the code below loop will naturally cause the error you mentioned in building 9. Or using the one in building 11 will work.
致精致简!
Floor 14 Posted 2008-05-26 23:13 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Thanks to 26933062. The reason I need such a batch is that I want to make a multi-in-one XP SP3 installation disc.

Most current multi-in-one discs have menus. I want to make one without a menu, which can automatically detect whether the machine is an OEM machine. If it is, install the corresponding OEM version (if the corresponding file is on the disc); otherwise, install the VOL version.

The advantage of no menu is that it can improve the boot compatibility of the disc.

I'm almost done. Actually, the program I need now won't have the disc removed during the actual system installation (but there's still the problem of dual CD-ROM drives), but I think someone might copy my part of the program to the hard drive and execute it, which would cause error messages. I find it very annoying, so I've been racking my brains to solve it.

I really learned something today!

In the process of realizing this idea, I used some DOS commands, all very crude commands, but the functions realized seem very complicated to me. 26933062, you're awesome. If you saw my messy code, you'd probably laugh.

[ Last edited by quya on 2008-5-26 at 11:16 PM ]
Floor 15 Posted 2008-05-26 23:33 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
You're welcome. When everyone helps each other, in fact, one is also improving oneself.
You've praised me too much. In this forum, there are many experts, how dare I talk about the word "strong" casually.
Take a good look at the old posts here, and you'll know what real strength is!
致精致简!
Forum Jump: