|
walu
高级用户
   
积分 916
发帖 201
注册 2003-5-4
状态 离线
|
『楼 主』:
一个简单批处理的难题
使用 LLM 解释/回答一下
一个简单批处理的难题
@ECHO OFF
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho GOTO label
goto err
:label
label %%i:SYSTEM
md %%i:\boot
copy aa.txt %%i:\boot
goto exit
:err
echo nofile
:exit
批处理的意思是:如果C分区存在SYS.GHO文件,就跳转到:label执行相应命令,不再继续查找其它分区,如此类推。。。。。。
但按上面的编写执行出错。不少网友给了不同的解决方案,但都没有正确的。
请willsort等高手出手!
A Simple Batch Processing Dilemma
@ECHO OFF
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho GOTO label
goto err
:label
label %%i:SYSTEM
md %%i:\boot
copy aa.txt %%i:\boot
goto exit
:err
echo nofile
:exit
The meaning of the batch processing is: If the C partition has the SYS.GHO file, jump to :label to execute the corresponding commands and no longer continue to search other partitions, and so on...
But when executed according to the above writing, an error occurs. Many netizens have given different solutions, but none are correct.
Please ask experts like willsort to come to the rescue!
|
|
2004-5-30 00:00 |
|
|
haonanren
初级用户
 
积分 162
发帖 14
注册 2004-5-16
状态 离线
|
|
2004-5-30 00:00 |
|
|
haonanren
初级用户
 
积分 162
发帖 14
注册 2004-5-16
状态 离线
|
|
2004-5-30 00:00 |
|
|
Tamm
中级用户
  
积分 397
发帖 64
注册 2004-4-18
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
你的批处理出错可能是因为: %%i 离开 FOR 语句以后将失效
你可尝试这样看行不行:(未经测试,我只是提供一个思路)
@ECHO OFF
SET SYS=
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho SET SYS=%%i
if %SYS%*==* goto err
GOTO :label
:label
label %SYS%: SYSTEM
md %SYS%:\boot
copy aa.txt %SYS%:\boot
goto exit
:err
echo nofile
:exit
Your batch processing, the error might be because %%i becomes invalid after leaving the FOR statement.
You can try seeing if this works: (not tested, I'm just providing an idea)
@ECHO OFF
SET SYS=
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho SET SYS=%%i
if %SYS%*==* goto err
GOTO :label
:label
label %SYS%: SYSTEM
md %SYS%:\boot
copy aa.txt %SYS%:\boot
goto exit
:err
echo nofile
:exit
|
|
2004-5-30 00:00 |
|
|
zhri
高级用户
    zhri
积分 665
发帖 153
注册 2004-2-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
if exist c:\SYS.GHO
。。。。。。
如果存在c:\SYS.GHO
。。。。。。
If exist c:\SYS.GHO
。。。。。。
|
|
2004-5-30 00:00 |
|
|
quya
高级用户
    五星老土
积分 558
发帖 172
注册 2003-2-9 来自 江苏
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
改成下面的就可以了,程序主要错在%%i 变量不能传递到 :label子程序中,所以要用一个全局环境变量%mydrive%来过渡一下。
@ECHO OFF
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho set mydrive=%%i
if not defined mydrive (goto err) else (goto label)
:label
label %mydrive%:SYSTEM
md %mydrive%:\boot
copy aa.txt %mydrive%:\boot
goto exit
:err
echo nofile
:exit
Just change it to the following. The main mistake in the program is that the %%i variable cannot be passed to the :label subroutine, so a global environment variable %mydrive% is used to transition.
@ECHO OFF
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho set mydrive=%%i
if not defined mydrive (goto err) else (goto label)
:label
label %mydrive%:SYSTEM
md %mydrive%:\boot
copy aa.txt %mydrive%:\boot
goto exit
:err
echo nofile
:exit
|

我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已. |
|
2004-5-30 00:00 |
|
|
walu
高级用户
   
积分 916
发帖 201
注册 2003-5-4
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
谢谢quya兄!
在WIN 98下运行如果只有C、D分区的情况下,显示“NOT READ READING DRIVE E”。
在WIN XP是有效,但如果后面的分区有BOOT。INI文件的话,即使C分区有BOOT。INI文件,它也检测到后面的分区,而不是检测到C分区有就结束。
真希望哪位兄弟能解决这个久未解决的问题,即既能在WIN XP下运行也能在WIN 98下运行。
Thanks, Brother quya!
When running under WIN 98, if there are only partitions C and D, it displays "NOT READ READING DRIVE E".
Under WIN XP, it is effective, but if there is a BOOT.INI file in the subsequent partition, even if there is a BOOT.INI file in partition C, it detects the subsequent partition instead of ending when it detects that there is one in partition C.
I really hope that some brother can solve this long-unresolved problem, that is, it can run under both WIN XP and WIN 98.
|
|
2004-5-30 00:00 |
|
|
Tamm
中级用户
  
积分 397
发帖 64
注册 2004-4-18
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
再推荐一个复杂但易于操作和理解的办法:
========================
主批处理中:
========================
@ECHO OFF
CALL TestDrv.bat C D E F G H I J K L M N O P Q R S T U V W X Y Z
IF %SysDrv%*==* GOTO err
GOTO label
:label
label %SysDrv%: SYSTEM
md %SysDrv%:\boot
copy aa.txt %SysDrv%:\boot
goto exit
:err
echo nofile
:exit
===========================
子批处理 TestDrv.bat 内容如下:
===========================
@ECHO OFF
SET SysDrv=
:LOOP
DReady %1
IF NOT ERRORLEVEL 1 IF EXIST %1:\BOOT.INI GOTO FOUND
SHIFT
IF %1*==* GOTO END
GOTO LOOP
:FOUND
SET SysDrv=%1
:END
========================
在子批处理中使用了一个 DReady.com 是 Horst's Batch Tools 中的一个小工具。用于测试驱动器状况,返回 ERRORLEVEL 表示驱动器是否准备好。
Here's the translation:
Another recommended complex but easy-to-operate and understand method:
========================
In the main batch processing:
========================
@ECHO OFF
CALL TestDrv.bat C D E F G H I J K L M N O P Q R S T U V W X Y Z
IF %SysDrv%*==* GOTO err
GOTO label
:label
label %SysDrv%: SYSTEM
md %SysDrv%:\boot
copy aa.txt %SysDrv%:\boot
goto exit
:err
echo nofile
:exit
===========================
The content of the sub-batch processing TestDrv.bat is as follows:
===========================
@ECHO OFF
SET SysDrv=
:LOOP
DReady %1
IF NOT ERRORLEVEL 1 IF EXIST %1:\BOOT.INI GOTO FOUND
SHIFT
IF %1*==* GOTO END
GOTO LOOP
:FOUND
SET SysDrv=%1
:END
========================
In the sub-batch processing, a DReady.com is used, which is a small tool in Horst's Batch Tools. It is used to test the drive status and returns ERRORLEVEL to indicate whether the drive is ready.
|
|
2004-5-30 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Re All:
walu兄对此系列问题的执著很令我钦佩。现将我在Email中所做的回复转贴于此,权作补遗。因为回复之前并未见到此帖,难免与各位方家观点有所重复,尚请无怪!
原程序中存在一个认识上的误区,那就是for循环中的变量只在循环语句中有效,在
循环语句之外是无法识别循环变量%%i的,此点与高级语言的局部变量类似。
了解问题的原因就可以想出简单的办法了,下面提供两种办法仅供参考:
1,利用环境变量保存循环变量。环境变量全局有效,而且存取简单,所以是一个非常
理想的保存临时数据或信息的方法。需要注意的是,此方法会始终搜索所有给出的逻辑盘,假
如d和f盘都符合条件,程序将取后者;如果想取前者,把循环集中的盘符列表逆序书写即可
。
@echo off
set drive=
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho set drive=%%i
if == goto err
:label
label %drive%:SYSTEM
md %dirve%:\boot
copy aa.txt %drive%:\boot
set drive=
goto exit
:err
echo nofile
:exit
2,利用命令行参数传递循环变量。命令行参数也是常用的变量类型之一,它可以方便
的向程序传递各种信息。此方法与1不同的是,它一旦匹配逻辑盘就立即跳出循环,所以它会
仅取第一个匹配的逻辑盘。如果想处理所有匹配的逻辑盘,可以将for语句中的%0 %%i 改为
call %0 %%i
@echo off
for %%i in (c d e f g h i j ) do if == goto label
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho %0 %%i
goto err
:label
label %1:SYSTEM
md %1:\boot
copy aa.txt %1:\boot
goto exit
:err
echo nofile
:exit
另外,以上所有的if exist %%i:\SYS.gho 都存在一个潜在的问题:如果 %%i 为光驱的
盘符,且此光驱中无盘的话,会出现读盘错误,影响程序的正常执行。这是一个比较复杂的问
题,也有多种解决办法,建议参考"中国DOS联盟论坛\解答室"中相关的帖子。
Re All:
Brother walu's perseverance in this series of issues is quite admirable to me. Now, I am reposting the reply I made in the email here as a supplement. Since I didn't see this post before replying, there may be some repetitions with the views of various experts. Please don't mind!
There is a misunderstanding in the original program, that is, the variable in the for loop is only valid within the loop statement, and the loop variable %%i cannot be recognized outside the loop statement, which is similar to the local variable of a high-level language.
Once the cause of the problem is understood, simple methods can be thought of. The following are two methods for reference:
1. Use environment variables to save the loop variable. Environment variables are globally effective and easy to access, so they are a very ideal method for saving temporary data or information. It should be noted that this method will always search all the given logical drives. If both drive D and drive F meet the conditions, the program will take the latter; if you want to take the former, write the drive letter list in the loop in reverse order.
@echo off
set drive=
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho set drive=%%i
if == goto err
:label
label %drive%:SYSTEM
md %dirve%:\boot
copy aa.txt %drive%:\boot
set drive=
goto exit
:err
echo nofile
:exit
2. Use command line parameters to pass the loop variable. Command line parameters are also one of the commonly used variable types, which can conveniently pass various information to the program. This method is different from 1 in that it immediately jumps out of the loop once a logical drive is matched, so it will only take the first matched logical drive. If you want to process all matched logical drives, change %0 %%i in the for statement to call %0 %%i
@echo off
for %%i in (c d e f g h i j ) do if == goto label
for %%i in (c d e f g h i j ) do if exist %%i:\SYS.gho %0 %%i
goto err
:label
label %1:SYSTEM
md %1:\boot
copy aa.txt %1:\boot
goto exit
:err
echo nofile
:exit
In addition, there is a potential problem in all the above if exist %%i:\SYS.gho: if %%i is the drive letter of a CD drive and there is no disk in this CD drive, a disk reading error will occur, which will affect the normal execution of the program. This is a relatively complex problem, and there are also various solutions. It is recommended to refer to the relevant posts in "China DOS Union Forum \ Q&A Room".
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2004-6-2 00:00 |
|