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-25 09:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Repost: Subroutines in Batch Commands View 2,281 Replies 2
Original Poster Posted 2002-10-25 00:00 ·  中国 广东 佛山 禅城区 电信
元老会员
★★★★
Credits 5,170
Posts 1,637
Joined 2002-10-16 00:00
23-year member
UID 8
Gender Male
From 广东佛山
Status Offline
Subroutines in Batch Commands

Most mature high-level programming languages allow you to build common routines into independent modules, namely subroutines, which preserve the independence of the code, standardize the program, and facilitate organization. At the very least, it can be said that DOS batch commands do not fully support subroutines, but you can always use the GOTO command to call a subroutine.

Usually, the label used by the GOTO command can also accept a label stored in an environment variable, for example:

SET LABELNAME=START

GOTO %LABELNAME%

By replacing the label at the beginning of the subroutine and setting the GOTO %RETURN% statement at the end of the subroutine, you can build subroutines in DOS batch commands. As long as you SET an environment variable RETURN, you can call the subroutine at any point in the batch command. For example, you can assign a value to the label and call the subroutine on the next line, then the GOTO statement jumps to the beginning of the subroutine. The program structure is as follows:

REM This set the environment
REM variable and calls the
REM subroutine
SET RETURN=HERE
GOTO SUB
HERE
SUB
REM
Place subroutine statements
REM below
GOYO %RETURN%

The statement GOTO SUB transfers to the subroutine SUB, where you can execute any statements. When executing to GOTO %RETURN%, the batch command returns control to the label HERE because the environment variable RETURN is reassigned to HERE. If you use a unique label (such as RETURN) each time you call SUB, you can call SUB multiple times in multiple places.
我的网志
http://hzmys.blog.163.com/
我的网盘
firststep.qjwm.com
fsmys.ys168.com
ssmys.ys168.com
www.brsbox.com/fsmys
www.brsbox.com/ssmys
www.brsbox.com/ccdos
Floor 2 Posted 2006-05-04 22:30 ·  中国 四川 成都 电信
初级用户
Credits 24
Posts 12
Joined 2006-05-04 09:41
20-year member
UID 54910
Status Offline
First post, not easy!
Floor 3 Posted 2006-05-05 19:40 ·  中国 福建 泉州 电信
高级用户
★★
论坛灌水专业户
Credits 613
Posts 266
Joined 2006-04-19 22:47
20-year member
UID 54113
From 河南省
Status Offline
饮马恒河畔,剑指天山西,碎叶城揽月,库叶岛赏雪,黑海之滨垂钓,贝尔加湖面张弓;中南半岛访古,东京废墟祭祖!
Forum Jump: