SoFunction
Updated on 2025-04-14

More detailed step-by-step instructions for writing batch processing (willsort title version) page 4/5


Example: The content is as follows (note that when using if errorlevel to determine the return value, you should arrange it from high to low according to the return value):
@echo off
choice /C dme /M "defrag,mem,end"
if errorlevel 3 goto end
if errorlevel 2 goto mem
if errotlevel 1 goto defrag

==== willsort Editor's Note =====================================================================
errotlevel -> errorlevel
========================================================================

:defrag
c:\dos\defrag
goto end

:mem
mem
goto end

:end
echo good bye

After this batch is run, "defrag,mem,end[D,M,E]?" will be displayed. The user can select d m e , and then the if statement makes a judgment based on the user's choice. d represents the execution block with the label defrag, m represents the execution block with the label mem, and e represents the execution block with the label end. Each program block finally jumps the program to the end label with goto end, and the program will display good bye, and the batch run ends.



4. For loop command, as long as the conditions meet, it will execute the same command multiple times.

grammar:
Execute a specific command on each file in a set of files.

FOR %%variable IN (set) DO command [command-parameters]

%%variable Specifies a single letter replaceable parameter.
(set) Specify one or a group of files. Wildcards can be used.
command Specifies the command to be executed on each file.
command-parameters
Specify parameters or command line switches for a specific command.

For example, there is a line in a batch file:
for %%c in (*.bat *.txt) do type %%c

Then this command line will display the contents of all files with bat and txt extensions in the current directory.

==== willsort Editor's Note =====================================================================
It should be pointed out that when the string in () is not a single or multiple file names, it will be simply replaced as a string. This feature plus the feature that can embed multiple strings in () makes it obvious that for can be regarded as a traversal loop.
Of course, in the command line environment of the nt/2000/xp/2003 series, for is given more features, allowing it to analyze command output or strings in files, and many switches are also used to extend the file replacement function.
========================================================================

--------------------------------------------------------
Title: Teach you step by step to write batch processing - Batch processing example
Author: Anonymous
Editor: Climbing
Source: China DOS Alliance Joint DOS Forum
Title: willsort
Date: November 10, 2004
--------------------------------------------------------

[Repost and improve] teach you how to write batch processing step by step

The original author has no professionalism in writing, and the articles he wrote are all kinds of mistakes. Moreover, if it is not modified and improved, it will be a misleading child. Therefore, I modified it based on the original text and corrected most of the errors (of course, there may be new errors, and I hope that the masters can point them out in time after discovering them).

URL: /News/technic/200408/

Excerpted from: World Internet Cafe Alliance Author: Anonymous
Modification and improvement: Climbing(xclimbing@)
Last modified: 19 August 2004

Batch Processing Example

1. IF-EXIST

1) First use Notepad to create a batch file in C:\, with the file contents as follows:
@echo off
IF EXIST \ TYPE \
IF NOT EXIST \ ECHO \ does not exist

Then run it:
C:\>

If C:\ file exists, then its content will be displayed. If it does not exist, the batch process will prompt you that the file does not exist.

2) Then create another file, the content is as follows:
@ECHO OFF
IF EXIST \%1 TYPE \%1
IF NOT EXIST \%1 ECHO \%1 does not exist

implement:
C:\>TEST2
The command run result is the same as above.

illustrate:
(1) IF EXIST is used to test whether the file exists, and the format is
IF EXIST [Path + File Name] Command
(2) %1 in the file is a parameter. DOS allows passing 9 batch parameter information to the batch file, which is %1~%9 (%0 represents the test2 command itself). This is a bit like the relationship between actual parameters and formal parameters in programming. %1 is a formal parameter, which is a real parameter.

==== willsort Editor's Note =====================================================================
DOS has no limit on "allowing to pass 9 batch parameter information", and the number of parameters will only be limited by the command line length and the processing capability of the command called. However, in the batch program, we can only refer to 10 parameters at the same time, because DOS only gives ten parameter reference characters: %0~%9.
========================================================================
Previous page12345Next pageRead the full text