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-13 02:23
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » A batch file question? Thanks View 626 Replies 2
Original Poster Posted 2003-12-09 00:00 ·  中国 广东 广州 白云区 电信
初级用户
Credits 130
Posts 6
Joined 2003-10-05 00:00
22-year member
UID 10729
Gender Male
Status Offline
I have a question:
About SETRAMD.BAT in the Win98 boot disk:
:loop
if errorlevel %1 goto no_shift

:do_shift
set cdrom=%2
shift
shift
if not %1*==* goto loop
goto no_ramdrive

:no_shift
set ramd=%2

How should the line %1*==* here be understood?
Why is there also a * after %1?

Thanks
Floor 2 Posted 2003-12-10 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re sizzling:

This is a defensive trick used to avoid a syntax error when a parameter is empty.

In the example above, if the IF line were changed to
if not %1== goto loop
it would not conform to the syntax rules, because DOS's command interpreter requires a non-empty string parameter immediately on both sides of ==. In other words, in batch processing there is no concept of an empty string, so we need to make some compromise:
if not %1*==* goto loop
This way, when %1 is empty, the above line is replaced with
if not *==* goto loop
the condition is true and the jump is executed; otherwise it is false, and execution continues with the next statement.

Also, * is not the only character that can be used for defensive design. It can be replaced completely with any other character or string, as long as both sides of == remain consistent. Many programmers used to high-level languages are accustomed to the following format:
if not ''%1''=='''' goto loop
or
if not "%1"=="" goto loop






※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2003-12-10 00:00 ·  中国 广东 广州 电信
初级用户
Credits 130
Posts 6
Joined 2003-10-05 00:00
22-year member
UID 10729
Gender Male
Status Offline
Thanks to willsor t for the guidance!
Now I finally understand it.
Mainly because I overlooked the case when %1 is empty! Haha
Forum Jump: