中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-23 13:55
47,812 topics / 349,917 posts / today 0 new / 48,273 members
DOS批处理 & 脚本技术(批处理室) » A Dilemma with a Simple Batch Processing
Printable Version  1,996 / 8
Floor1 walu Posted 2004-05-30 00:00
高级用户 Posts 201 Credits 916
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!

Floor2 haonanren Posted 2004-05-30 00:00
初级用户 Posts 14 Credits 162
It seems there is no problem.
Floor3 haonanren Posted 2004-05-30 00:00
初级用户 Posts 14 Credits 162
Floor4 Tamm Posted 2004-05-30 00:00
中级用户 Posts 64 Credits 397
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

Floor5 zhri Posted 2004-05-30 00:00
高级用户 Posts 153 Credits 665
如果存在c:\SYS.GHO

。。。。。。

If exist c:\SYS.GHO

。。。。。。
Floor6 quya Posted 2004-05-30 00:00
高级用户 Posts 172 Credits 558 From 江苏
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




Floor7 walu Posted 2004-05-30 00:00
高级用户 Posts 201 Credits 916
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.

Floor8 Tamm Posted 2004-05-30 00:00
中级用户 Posts 64 Credits 397
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.
Floor9 willsort Posted 2004-06-02 00:00
元老会员 Posts 1,512 Credits 4,432
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".

[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023