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-01 13:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please ask again, I can't answer this question now. Please provide other topics for me to answer. View 1,702 Replies 10
Original Poster Posted 2005-10-29 20:50 ·  中国 河北 沧州 联通
中级用户
★★
MS-DOS爱好者
Credits 397
Posts 87
Joined 2002-12-15 00:00
23-year member
UID 517
Gender Male
Status Offline
Recently I wrote a batch program, but every time it reaches this part, something is wrong:

*******************************
echo Which partition do you want to restore the system to (please enter the partition number or drive letter)?: (123456789 or DEFGHIJKL)
set WHAT=
what s
for %%a in (D E F G H I J K L) do IF == goto _copy
for %%b in (1 2 3 4 5 6 7 8 9) do IF == set HDRV=1:%%b
ghost -clone,mode=pload,src=%CDROM%\sysbak.GHO:1,dst=%HDRV% -sure -rb
goto to_BOOT

:_copy
for %%c in (C D E F G H I J K L) do IF == set HDRV=%WHAT%
sys %HDRV%:
copy /y a:\gho\*.*\ %HDRV%

************************************

Later, I found that the value in the variable %HDRV% is incorrect, it's empty. It seems these two sentences are wrong:

for %%a in (D E F G H I J K L) do IF == goto _copy
for %%b in (1 2 3 4 5 6 7 8 9) do IF == set HDRV=1:%%b

Please brothers help me see where the problem is? Why?


—————————————— Edited by willsort ——————————————
Original title: Please brothers help me see where this batch is wrong.
To facilitate users' browsing and moderators' management, please briefly describe its content or intention in the post title
You can click the "Edit" button below the post to modify the title on the editing page and confirm it
If no modification is made within three days, the moderator will modify it, and the author of this post will be deducted 4 points of points at the same time
—————————————— Edited by willsort ——————————————


[ Last edited by sglxy on 2005-11-1 at 23:51 ]
Floor 2 Posted 2005-10-29 22:44 ·  中国 河北 沧州 任丘市 华北油田网络
中级用户
★★
MS-DOS爱好者
Credits 397
Posts 87
Joined 2002-12-15 00:00
23-year member
UID 517
Gender Male
Status Offline
Hello, moderators. Is anyone online? Please take a look for me. Thanks in advance.
Floor 3 Posted 2005-10-30 19:51 ·  中国 河北 沧州 任丘市 华北油田网络
中级用户
★★
MS-DOS爱好者
Credits 397
Posts 87
Joined 2002-12-15 00:00
23-year member
UID 517
Gender Male
Status Offline
What a pity, the experts are all not here!
Floor 4 Posted 2005-10-30 23:06 ·  中国 广东 广州 海珠区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
Try to change [%WHAT%] to [%%WHAT%%]
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 5 Posted 2005-10-31 12:03 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 2,227
Posts 790
Joined 2005-01-27 00:00
21-year member
UID 35703
Gender Male
Status Offline
Re sglxy:

There is a problem with the usage of what in your case. It should not be what S, but what C "prompt message" character list. The C command will automatically limit the range of input characters, so there is no need to perform IF tests anymore. Modify as follows:
*******************************
set WHAT=
what C "Which partition do you want to restore the system to (please enter the partition number or drive letter)? (123456789 or DEFGHIJKL):" 123456789DEFGHIJKL
for %%a in (D E F G H I J K L) do goto _copy
for %%b in (1 2 3 4 5 6 7 8 9) do set HDRV=%%b
ghost -clone,mode=pload,src=%CDROM%\sysbak.GHO:1,dst=1:%HDRV% -sure -rb
goto to_BOOT

:_copy
for %%c in (C D E F G H I J K L) do set HDRV=%WHAT%
sys %HDRV%:
copy /y a:\gho\*.*\ %HDRV%
*******************************

[ Last edited by 本是 on 2005-10-31 at 12:44 ]
my major is english----my love is dos----my teacher is the buddha----my friends--how about U
Floor 6 Posted 2005-10-31 12:56 ·  中国 河北 沧州 任丘市 华北油田网络
中级用户
★★
MS-DOS爱好者
Credits 397
Posts 87
Joined 2002-12-15 00:00
23-year member
UID 517
Gender Male
Status Offline
Thanks to brother "Ben Shi" for the reply, but I still have a question, please see:

for %%a in (D E F G H I J K L) do goto _copy

Is the syntax a bit awkward after such a change?
Floor 7 Posted 2005-10-31 14:16 ·  中国 山西 太原 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re sglxy & Ben Shi:

Brother sglxy's original code will cause problems, which should be related to the %what% variable. As for the correctness of the usage of what s, since I haven't been in contact with this tool for a long time, I can't comment; but I guess what s is for accepting string input, so it should be able to achieve your intention.

for %%a in (D E F G H I J K L) do IF [%%a]==[%WHAT%] goto _copy not only "limits the range of input characters", but also sorts the input characters. If it is a letter, it will goto _copy; if it is a number, it will ghost. Therefore, the if segment should not be omitted.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2005-10-31 16:36 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 2,227
Posts 790
Joined 2005-01-27 00:00
21-year member
UID 35703
Gender Male
Status Offline
What Willsort said, "for %%a in (D E F G H I J K L) do IF == goto _copy not only 'limits the range of input characters', but also sorts the input characters. If it is a letter, it goes to _copy; if it is a digit, it goes to ghost. Therefore, the if fragment should not be omitted." is very correct.

What command C can limit the range of input characters, while command S cannot. This is very important for this.BAT.

The problem here is probably due to the setting rules of environment variables in NT-series operating systems being different from those in WIN9X!

[ Last edited by 本是 on 2005-10-31 at 16:38 ]
my major is english----my love is dos----my teacher is the buddha----my friends--how about U
Floor 9 Posted 2005-10-31 18:27 ·  中国 河北 沧州 任丘市 华北油田网络
中级用户
★★
MS-DOS爱好者
Credits 397
Posts 87
Joined 2002-12-15 00:00
23-year member
UID 517
Gender Male
Status Offline
Thank you very much to the two experts for your guidance. This batch script is a part of the automatic recovery function for hiding partitions that I made. The method is to first: run gdisk to display partition information, and then manually enter which partition to operate on. Then the what s command passes the input key value to the variable %WHAT% in the for %%a in (D E F G H I J K L) do IF == goto _copy for judgment. If it is one of them, it means that the partition is not hidden (the partition will not have a drive letter after being hidden), and then I can install some personalized recovery functions on this partition. But this method that I thought was foolproof偏偏 doesn't get the expected result. Now I don't understand why the judgment sentence for %%a in (D E F G H I J K L) do IF == goto _copy doesn't work. I think its syntax should be correct.

In addition, I would like to ask the "Ben Shi" brother, in the case of needing to limit the input character range, how to write the "what c" command (syntax)? What is the resulting variable form, and how should this variable be operated? Please give an example, thank you.

[ Last edited by sglxy on 2005-10-31 at 18:38 ]
Floor 10 Posted 2005-11-01 13:37 ·  中国 河北 石家庄 联通
新手上路
Credits 3
Posts 2
Joined 2005-11-01 11:55
20-year member
UID 44402
Status Offline
I have a personal programming habit, which is to display one by one the parameters it calls in front of the erroneous statement. I wonder if you have tried it
Floor 11 Posted 2005-11-02 12:55 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re sglxy:

The usage of what c was already given by brother in floor 5. The last parameter 123456789DEFGHIJKL is the one that limits its input range. For other usages of what, it should not be difficult to understand by looking at its command line help.

The line for %%a in (D E F G H I J K L) do IF [%%a]==[%WHAT%] goto _copy has no grammatical errors, but it is not guaranteed to have no usage errors. If the variable value of %WHAT% has spaces before or after, then you will never make the if judgment hold. So you should follow Archimonde's suggestion, add a line echo what result=[%what%] before the for statement to check if its output result is normal. If this result is normal, then the problem may be in other statements.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: