"Will Sort from China" wrote in message
Post by Will Sort from ChinaThank you for taking the time to reply and share your knowledge.
This newsgroup is for sharing knowledge :-)
Post by Will Sort from ChinaI am a Chinese and my English is poor.
Your English is better than my Chinese (=I speak no Chinese at all).
Post by Will Sort from ChinaI hope that you understand much phase problem of this text.
Post by William AllenIt's conventional to have a Batch file delete its workfiles when it
finishes (unless you want to retain them for diagnostic or future use).
I know that deleting of workfiles is necessary in most of batch
program.
If delete workfiles in batch, errorlevel will set to 0 by 'del'
command.
Yes. That's a good point. I tried to find a solution to that problem.
As far as I can tell, the START command can be used to avoid any
change to the current ERRORLEVEL in CMD.EXE, and this can be
used to prevent DEL changing the ERRORLEVEL you have set.
This example shows how to delete a workfile without affecting the
current ERRORLEVEL by running DEL in a START /min CMD /c shell.
You can read details of START command with /? help switch:
start /?
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
:: Create a pretend workfile
ECHO. Create a pretend workfile>MyWorkFile.TXT
:: Set ERRORLEVEL 1 with a bad DIR command and redirect
:: the error message to NUL with: >NUL 2>&1
DIR Bad:FileName >NUL 2>&1
ECHO. Bad DIR command set Errorlevel=%ERRORLEVEL%
:: Use START /min to run an instance of CMD.EXE to execute a DEL
:: In my simulated Windows 2000, this doesn't affect ERRORLEVEL
start /min cmd /c DEL MyWorkFile.TXT
:: Check ERRORLEVEL again
ECHO. Errorlevel after START DEL is still=%ERRORLEVEL%
====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
This screen capture shows how it works for me:
============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo.cmd
Bad DIR command set Errorlevel=1
Errorlevel after START DEL is still=1
C:\WORK>
============End screen capture
This example above also shows a way many people use to post Batch files
to NewsGroups. Each line begins with two [Space]s so that if a line is
wrapped by accident, you can easily see that it has happened. When you
posted your Batch file, some lines wrapped (broke into two parts).
So for example, one of your lines appeared to me as:
===From Will Sort
if errorlevel 10 for %%n in (%#el%) do if errorlevel %el%%%n0 set
el=%el%%%n
===
Of course, I knew that it was meant to be on one line, but someone
who did not know much Batch language may not see this and
would not be able to get the Batch file to work properly.
If you indent all lines by two [Space]s, then if these two lines
wrapped, it would show as:
===
if errorlevel 10 for %%n in (%#el%) do if errorlevel %el%%%n0 set
el=%el%%%n
===
and it would be more obvious that the second line was an overflow
and not really a separate line at all.
...snip
Post by Will Sort from ChinaIn Win9x command, I wrote a program on set errorlevel too.
====Begin cut-and-paste (omit this line)
:: SetErr.bat - Second version
@echo off
if [%1]==[] goto end
:main
echo a>_SetErr.scr
echo mov ax,4c%1>>_SetErr.scr
echo int21>>_SetErr.scr
echo.>>_SetErr.scr
echo g>>_SetErr.scr
echo q>>_SetErr.scr
debug <_SetErr.scr> nul
if not errorlevel 1 echo Invalid argument '%1'
:: can't check errorlevel of hexnum form
:end
====End cut-and-paste (omit this line)
That's good.
As I said, if you indent each line of the posted Batch file by two [Space]s
between the === lines and it will be obvious to anyone using the
Batch file if a line has accidentally wrapped (=broken in two parts).
You can make your example a little better by using two separate
assembly commands:
mov ah,4c
mov al,%1
Then, if someone forgets to put a leading 0 in front of a small
errorlevel such as 05, maybe they type:
demo.bat 5
by mistake instead of
demo.bat 05
then the Batch file won't go wrong.
And if you are a little more subtle, you can also handle the case
where they do not type any parameter. If you make those two mov
lines become:
mov ax,0%1
mov ah,4c
then when the Batch file is used with no parameter, it sets an
ERRORLEVEL of zero. So if they type:
demo.bat
it will set ERRORLEVEL 0 without needing to test whether there
is a %1 parameter or not.
===Using the PROMPT to write small DEBUG scripts
In Windows 95/98/ME, you can also use the PROMPT command to write
small DEBUG scripts in a very compact way. If you type:
prompt /?
you'll see the special characters that the PROMPT can include. One
of them is $_ (which means Carriage Return Linefeed = a new line).
This means that if you want to use a DEBUG script such as:
===Script starts
a
mov ax,0%1
mov ah,4c
int 21
g
q
===Script ends
you can generate it all as a PROMPT in one line, with:
PROMPT a$_mov ax,0%1$_mov ah,4c$_int 21$_$_g$_q
where $_ means start a new line.
If you run this PROMPT through COMMAND.COM (usually as %COMSPEC% in
batch files) you can ECHO it through DEBUG. You have to ECHO the
special PROMPT command into a temporary Batch file, then run the
Batch file in a %COMSPEC%/c child shell and pipe it to DEBUG.
This shows the syntax to set the ERRORLEVEL to the hex value in
the %1 parameter (the same as your example Batch file):
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
:: Sets ERRORLEVEL to hex value in %1
***@PROMPT a$_mov ax,0%1$_mov ah,4c$_int 21$_$_g$_q>%TEMP%.\_ERRL.BAT
%COMSPEC%/e:1024/c %TEMP%.\_ERRL.BAT | debug>NUL
DEL %TEMP%.\_ERRL.BAT
====End cut-and-paste (omit this line)
For Win95/98/ME study/demo use. Cut-and-paste as plain-text Batch file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
The temporary Batch file is created in the TEMP folder, which is variable
usually set to C:\WINDOWS\TEMP in Windows 95/98/ME. Strictly speaking,
you don't need the [Space]s in mov[Space]ax and int[Space]21 and so on,
but I put them in for clarity.
--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/