Batch file production examples wonderful tutorials 1th/5 page _dos/bat

Source: Internet
Author: User
Tags echo command error code goto
Batch files are unformatted text files, and now diskless cleanup is mostly achieved because it's simple and practical to make it easier to create batch files.


Brief introduction to a simple batch process internal command

1.Echo command
Turn on Echo or turn off request echoing, or display a message. If there are no arguments, the echo command displays the current echo setting.
Grammar
echo [{onoff}] [message]
Sample: @echo Off/echo Hello World
In practical applications we will combine this command with a redirect symbol (also known as a pipe symbol, commonly used in > >> ^) to implement input commands into a file in a particular format. This will be reflected in future examples.
2.@ command
Represents a command that does not show the @ behind it, and in the course of an intrusion (for example, using batches to format an enemy's hard disk), you can't let the other person see the command you're using.
Sample: @echo off
@echo now initializing the program,please wait a minite ...
@format X:/q/u/autoset (Format This command is not allowed to use/y This parameter, it is gratifying that Microsoft left a autoset This parameter to us, the effect and/y is the same. )

3.Goto command
Specifies that the program will process commands that start on the next line when the label is found.
Syntax: Goto label (label is a parameter that specifies the line in the batch program to which you want to turn.) )
Sample:
if {%1}=={} goto noparms
if {%2}=={} goto noparms (if,% 1,%2 you do not understand, skip first, then there will be a detailed explanation later.) )
@Rem check parameters if NULL show usage
: noparms
echo Usage:monitor.bat ServerIP portnumber
Goto END
The name of the label can be casually played, but it is best to have a meaningful letter, add a letter: to indicate that the letter is a label, the Goto command is based on this: to find the next jump to there. It's better to have some explanations so that you will be able to understand your intentions.

4.Rem command
Note Command, in the C language is equivalent to/*--------* *, it will not be executed, just play a role in the annotation, easy to read and your own future changes.
Rem message
Sample: @Rem This is the description.

5.Pause command
When you run the Pause command, the following message is displayed:
Press any key to continue ...
Sample:
@echo off
: Begin
Copy a:*.* D://back
echo please put a new disk into driver a
Pause
Goto BEGIN
In this example, all files on the disk in drive A are copied to D://back. When you display a comment that prompts you to put another disk in drive A, the pause command suspends the program so that you can replace the disk and press any key to continue processing.

6.Call command
Calls another batch program from one batch program and does not terminate the parent batch program. The call command accepts the label used as the calling target. If you use call outside of a script or batch file, it will not work on the command line.
Grammar
call [[Drive:][path] FileName [batchparameters]] [: Label [arguments]]
Parameters
[Drive:} [Path] FileName
Specifies the location and name of the batch program to invoke. The filename parameter must have a. bat or. cmd extension.

7.start command
Calling external programs, all DOS commands and command-line programs can be invoked by the start command.
Intrusion Common parameters:
Min Start window minimized
Separate starts 16-bit Windows programs in separate spaces
High priority class Start application
Realtime starts the application in the Realtime priority category
Wait to start the application and waiting for it to end
Parameters these parameters for transmission to the command/program
Executing the application is a 32-bit GUI application when CMD. EXE does not wait for application termination to return a command prompt. If executed within a command script, the new behavior does not occur.
8.choice command
Choice Use this command to allow the user to enter a character to run different commands. Use should be added/C: parameter, the letter should be written after the prompt can enter characters, no spaces between. Its return code is 1234 ...


such as: Choice/c:dme defrag,mem,end
will display
Defrag,mem,end[d,m,e]?
Sample:
The contents of Sample.bat are as follows:
@echo off
Choice/c:dme Defrag,mem,end
if errorlevel 3 goto defrag (the error code with the highest value should be judged first)
if errorlevel 2 goto MEM
If Errotlevel 1 goto end

:d Efrag
C://dos//defrag
Goto END
: Mem
Mem
Goto END
: End
echo Good bye

When this file is run, will display defrag,mem,end[d,m,e]? The user may select D m e, and then the IF statement will make a judgment, d means the execution label is defrag the program segment, m means the execution marking is the program segment of Mem, E represents the execution label as end of the program segment, each program paragraph ends with Goto ended the program jumps to the final label place, The program then displays the good bye and ends the file.

9.If command

If the condition is judged to be in compliance with the stipulated conditions, it is decided to execute different commands. There are three different formats:
1, if "parameter" = = "string" command to be executed
If the parameter is equal to the specified string, the condition is set, the command is run, or the next sentence is run. (note is two equals)
If "%1" = = "A" format a:
if {%1}=={} goto noparms
if {%2}=={} goto noparms

2, if exist file name to execute command
If you have the specified file, the condition is set, run the command, or run the next sentence.
such as if exist Config.sys edit Config.sys

3, if errorlevel/if not errorlevel number of commands to be executed
If the return code equals the specified number, the condition is set, the command is run, or the next sentence is run.
if errorlevel 2 goto x2
DOS program will return a number to DOS, called error code ERRORLEVEL or return code, the common return code is 0, 1.

10.for command
The for command is a more complex command that is used primarily to loop through commands within a specified range.
When you use the for command in a batch file, specify the variable using the%%variable

for {%variable%%variable} in (set) do command [CommandLineOptions]
%variable specifies a single letter replaceable parameter.
(set) to specify one or a set of files. You can use wildcard characters.
command specifies the commands that are executed for each file.
command-parameters specifies a parameter or command-line switch for a particular command.
When you use the for command in a batch file, specify the variable using the%%variable
Instead of using%variable. Variable names are case-sensitive, so%i are different from%i

If the command extension is enabled, the following additional for command formats are
Current 1/5 page 12345 Next read the full text

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.