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-06-29 00:13
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » Question about recursion in batch files View 886 Replies 2
Original Poster Posted 2005-06-03 00:00 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
The system I'm using is win2000
Right now I have 1.bat and 2.bat
The contents of 1.bat are:
del 1.txt
set a=10
set b=1
call 2.bat
echo end>>1.txt
The contents of 2.bat are:
echo %%%a%%%>>1.txt
set /a a=a-1
if %a% GTR 0 call 2.bat
echo %%%b%%%>>1.txt
set /a b=b+1
Execution result:
%10%
%9%
%8%
%7%
%6%
%5%
%4%
%3%
%2%
%1%
%1%
%2%
%3%
%4%
%5%
%6%
%7%
%8%
%9%
%10%
end
If I change %%%a%%% in 2.bat to %a%, and %%%b%%% to %b%
the result becomes:
10
ECHO is on.
ECHO is on.
10
end
In the black command window:
C:\>del 1.txt
C:\>set a=10
C:\>set b=1
C:\>call 2.bat
C:\>echo 10 1>>1.txt
C:\>set /a a=a-1
C:\>if 9 GTR 0 call 2.bat
C:\>echo 9>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 8 GTR 0 call 2.bat
C:\>echo 8>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 7 GTR 0 call 2.bat
C:\>echo 7>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 6 GTR 0 call 2.bat
C:\>echo 6>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 5 GTR 0 call 2.bat
C:\>echo 5>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 4 GTR 0 call 2.bat
C:\>echo 4>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 3 GTR 0 call 2.bat
C:\>echo 3>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 2 GTR 0 call 2.bat
C:\>echo 2>>1.txt
ECHO is on.
C:\>set /a a=a-1
C:\>if 1 GTR 0 call 2.bat
C:\>echo 1>>1.txt
C:\>set /a a=a-1
C:\>if 0 GTR 0 call 2.bat
C:\>echo 1>>1.txt
C:\>set /a b=b+1
C:\>echo 2>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 3>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 4>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 5>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 6>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 7>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 8>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 9>>1.txt
ECHO is on.
C:\>set /a b=b+1
C:\>echo 10 1>>1.txt
C:\>set /a b=b+1
C:\>echo end 1>>1.txt
I don't know where it's wrong
Please help, experts
Floor 2 Posted 2005-06-03 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 smileseeker:
  In the NT-series command line, if you put a number before the redirection symbol, it indicates the handle of the device being redirected, so you just need to add a space after %a% and %b%.
By default, command input can be sent from the keyboard (that is, the STDIN handle) to Cmd.exe, and then Cmd.exe sends command output (that is, the STDOUT handle) to the command prompt window.
The table below lists the available handles.
Handle Numeric handle code Description
STDIN 0 Keyboard input
STDOUT 1 Output to the command prompt window
STDERR 2 Error output to the command prompt window
UNDEFINED 3-9 These handles are defined separately by applications and individual tools
The numbers 0 through 9 represent the first 10 handles. You can use the command Cmd.exe to run a program and redirect any of the first 10 handles to that program. To specify the handle you want to use, type that handle's number before the redirection operator. If the handle is undefined, then the default < input redirection operator is 0, and the default > output redirection operator is 1. After typing the > or < operator, you must specify where to read or write the data. You can specify a file name or another existing handle.

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2005-06-03 00:00 ·  中国 福建 三明 电信
中级用户
★★
Credits 316
Posts 83
Joined 2005-03-01 00:00
21-year member
UID 36626
Gender Male
Status Offline
<!>
Forum Jump: