[Win] [Bat] common batch processing commands -- For, if, call, errorlevel, start

Source: Internet
Author: User

Run the batch processing command ---- for, if, call, errorlevel, start


Sample: 
Move files in batches and display the files that have been moved. Different prompts are displayed for success and failure.

Emuleok. BAT: http://download1.csdn.net/down3/20070518/18220919835.bat

Movefile. BAT: http://download1.csdn.net/down3/20070518/18221049118.bat

Emuleok. bat

@ Echo off
@ Echo move start
For/r f: eMule % I in (*. *) Do call movefile % I f: Download
If not errorlevel 0 @ echo move failed!
If errorlevel 0 @ echo move successfully
@ Echo on
@ Pause

Movefile. bat

@ Move % 1% 2
@ Echo move successfully: % 1

If command
If indicates whether the specified conditions are met, and then different commands are executed. There are three formats:

1. If "parameter" = "string" command to be executed

If the parameter is equal to the specified string, the condition is true. Run the command. Otherwise, run the next sentence. (Note that there are two equal signs)

For example, if "% 1" = "A" format:

If {% 1 }={} goto noparms

If {% 2 }={} goto noparms

2. If exist file name command to be executed

If a specified file exists, the condition is true. Run the command. Otherwise, run the next sentence.

For example, if exist config. sys edit config. sys

3. If errorlevel/if not errorlevel number command to be executed

If the return code is equal to the specified number, the condition is true. Run the command. Otherwise, run the next sentence.

For example, if errorlevel 2 goto X2

When the DOS program runs, a number is returned to DOS, which is called the error level or return code. Common return codes are 0 and 1.

 

For command
The for command is a complex command. It is mainly used to execute commands cyclically within a specified range of parameters.

When using the for command in a batch file, use % variable to specify the variable

For {% variable} In (SET) do command [commandlineoptions]

% Variable specifies a single letter replaceable parameter.

(SET) specifies one or more files. Wildcard characters can be used.

Command specifies the Command executed on each file.

Command-parameters specifies a parameter or command line switch for a specific command.

When using the for command in a batch file, use % variable to specify the variable

Instead of % variable. Variable names are case sensitive, so % I is different from % I

If the command extension is enabled, the following additional for command formats are subject

Supported:

For/d % variable in (SET) do command [command-parameters]

If the set contains wildcards, it is specified to match the directory name instead of the file name.

For/R [[drive:] path] % variable in (SET) do command [command-

Check the directory tree with [drive:] path as the root and point to the for statement in each directory. If no directory is specified after/R, the current directory is used. If the set is only a single point (.) character, the directory tree is enumerated.

For/L % variable in (START, step, end) do command [command-Para

This set indicates a sequence of numbers from start to end in incremental form.

Therefore, (, 5) will generate the sequence 1 2 3 4 5, (5,-) will generate

Sequence (5 4 3 2 1 ).

For/f ["options"] % variable in (file-set) do command

For/f ["options"] % variable in ("string") do command

For/f ["options"] % variable in (command) do command

Or, if usebackq is available:

For/f ["options"] % variable in (file-set) do command

For/f ["options"] % variable in ("string") do command

For/f ["options"] % variable in (command) do command

Filenameset is one or more file names. Continue to filenameset

Before the next file, each file has been opened, read, and processed.

Processing includes reading files, dividing them into lines of text, and then dividing each line

It is parsed into zero or more symbols. Then use the variable value of the found symbol string

Call the for loop. By default,/F is separated by each row of each file.

. Skip blank lines. You can specify the Optional options"

The parameter replaces the default parsing operation. This quoted string contains one or more

Specify keywords for different resolution options. These keywords are:

EOL = C-refers to the end of a line comment character (just one)

Skip = N-indicates the number of rows ignored at the beginning of the file.

Delims = xxx-refers to the delimiter set. This replaces the space and the hop key.

Default delimiter set.

Tokens = x, y, M-n-indicates which symbol of each row is passed to each iteration.

For itself. This causes

Format is a range. Specify m using the nth symbol

The asterisks of the last character in the symbol string,

Then the extra variables will be parsed In the last symbol

Distribute and accept reserved text of rows.

Usebackq-specify that the new syntax is used in the following situations:

Run a character string that is enclosed in quotation marks as a text string and allow the file name to be expanded using double quotation marks in FI.

 

Start command

Start another window to run the specified program or command.

Start ["title"] [/dpath] [/I] [/min] [/Max] [/separate |/shared]
[/Low |/normal |/high |/realtime |/abovenormal |/below
[/Wait] [/B] [command/Program]
[Parameters]

The title displayed in the title bar of the window.
Path start directory
B. Start the application without creating a new window. Unless
Start ^ C processing, otherwise the application will ignore ^ C processing;
^ Break is the only way to interrupt the application.
The new environment is the original environment passed to cmd.exe,
Instead of the current environment
Minimum window size when Min starts
Maximum window size when Max starts
Separate starts a 16-bit windows program in a separate space
Shared starts a 16-bit windows program in the shared space.
Low starts applications in the idle priority category
Normal starts the application in the normal priority category
High starts applications in the high priority category
Realtime starts applications in the realtime priority category
Abovenormal application started in the priority category of abovenormal
Belownormal starts applications in the priority category of belownormal
Wait starts the application and waits for it to end
Command/Program
If it is an internal cmd command or a batch file, the command processor is
Run cmd.exe with/K command lines. This indicates that the window is killed
Still exist.

If it is not an internal cmd command or a batch file, it is a program and
Run as a window application or console application.

Parameters: these are parameters sent to the command/program.

If the command extension is enabled, use an external command of the command line or start command.
The call will change as follows:

Type the file name as a command. Non-executable files can be called through file association.
(For example, word. Doc calls the application associated with the. DOC file extension ).
For more information about how to create these associations from the command script, see assoc and
FTYPE command.

When the executed application is a 32-bit GUI application, the cmd. EXE does not support the application.
If the program is terminated, a command prompt is returned. If it is executed in the Command Script, the new behavior
This will not happen.

If the first symbol of the executed command line does not contain an extension or path modifier
The string "cmd" and "cmd" will be replaced by the value of the comspec variable. This
Prevents cmd. EXE from being captured from the current directory.

If the first symbol of the command line does not have an extension, cmd. EXE uses
The value of the pathext environment variable determines the order in which extensions are to be searched.
Name. The default value of the pathext variable is:

. Com;. EXE;. BAT;. cmd

Note that this syntax is the same as that of the PATH variable. Different elements are separated by semicolons.

When searching for an executable file, if there is no matching extension, check whether the name is
Matches the directory name. If so, start will call
Explorer. If you execute from the command line, the path is equivalent to CD/d.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.