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 11:23
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A Dilemma with a Simple Batch Processing View 1,879 Replies 8
Original Poster Posted 2004-05-30 00:00 ·  中国 广东 河源 电信
高级用户
★★
Credits 916
Posts 201
Joined 2003-05-04 00:00
23-year member
UID 1849
Gender Male
Status Offline
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!

Floor 2 Posted 2004-05-30 00:00 ·  中国 广东 广州 联通
初级用户
Credits 162
Posts 14
Joined 2004-05-16 00:00
22-year member
UID 24476
Gender Male
Status Offline
It seems there is no problem.
Floor 3 Posted 2004-05-30 00:00 ·  中国 广东 广州 联通
初级用户
Credits 162
Posts 14
Joined 2004-05-16 00:00
22-year member
UID 24476
Gender Male
Status Offline
Floor 4 Posted 2004-05-30 00:00 ·  中国 湖南 株洲 电信
中级用户
★★
Credits 397
Posts 64
Joined 2004-04-18 00:00
22-year member
UID 22767
Gender Male
Status Offline
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

Floor 5 Posted 2004-05-30 00:00 ·  中国 上海 鹏博士宽带
高级用户
★★
zhri
Credits 665
Posts 153
Joined 2004-02-23 00:00
22-year member
UID 18241
Gender Male
Status Offline
如果存在c:\SYS.GHO

。。。。。。

If exist c:\SYS.GHO

。。。。。。
Floor 6 Posted 2004-05-30 00:00 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
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




我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 7 Posted 2004-05-30 00:00 ·  中国 广东 河源 电信
高级用户
★★
Credits 916
Posts 201
Joined 2003-05-04 00:00
23-year member
UID 1849
Gender Male
Status Offline
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.

Floor 8 Posted 2004-05-30 00:00 ·  中国 湖南 株洲 电信
中级用户
★★
Credits 397
Posts 64
Joined 2004-04-18 00:00
22-year member
UID 22767
Gender Male
Status Offline
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.
Floor 9 Posted 2004-06-02 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 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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: