Getting Started with batch processing and improving _dos/bat

Source: Internet
Author: User
Tags goto numeric value
Do you know the default sharing? It's not a good thing to have a place that doesn't need it. So I'm going to delete it. But this thing is recreated every time the system restarts. So every time I have to reopen cmd and delete it again. Extremely troublesome. But with a batch file is not the same, first the command entered in the batch file, and then added to the startup item, each start will automatically run, eliminating the trouble of each input command.
How do I create a batch file?
Open Notepad, write nothing, and then select the file to save. Save type Select all files, the filename is named *.bat this * represents the filename, you can casually start. Once you've saved it, look where you saved it, and there's a yellow gear icon in a white window. This is the batch file you created, double-click it to run it, but now he doesn't have any commands in it, so he runs and won't do anything. When we want to add something to this *.bat file, just right-click on it and select Edit to open Notepad to enter the command.
What are the commands in the batch file?
The commands in the batch file can be understood as DOS commands for the time being, and then explained later after deep understanding. Batch processing as the name suggests is a lot of things piled together. In other words is to write a DOS command inside, and then in order to execute, the effect with you in CMD to knock DOS command is an effect. Only after the batch is written, you can run it just by double-clicking it. Instead of repeating it over and over again to make a command. This is the benefit of the batch file.
In addition to running DOS commands, he can also support the choice of structure if, loop structure for,goto, and so on, and c a bit similar, but far from c comprehensive, and writing language is very irregular.
Batch syntax:
Let's talk about the basics @echo off.
echo means to maneuver, and here's what it means to echo, and echo off means to turn off the echo. The @ above @ means that the echo off line does not echo. You can try to remove the @ and the whole line. Another function of @ is to automatically restore command echo when the batch file is executed. If the first sentence uses echo off, the command prompt will not be displayed after the batch file is executed.
For example: If we first create a 1.bat file, enter it inside:
Dir is then saved under C:\. Then we run CMD, enter the C-packing directory, enter 1.bat, then show:
The volume in C:\>dir drive C does not have a label. The serial number of the volume is 0c5d-07ff
C:\ The directory
2004-08-25 00:45 <DIR> windows2004-08-25 00:51<dir> Documents and Settings
.....
C:\
If you modify the 1.bat content into
echo off
Dir
And then enter 1.bat in CMD, it will show
C:\>echo off
Because the echo off is running, the dir command is not displayed, and the results directly show that the volume in drive C is not labeled. The serial number of the volume is 0c5d-07ff
C:\ The directory
2004-08-25 00:45 <DIR> windows2004-08-25 00:51<dir> Documents and Settings
.....
C:\
If the 1. BAT file modified to:
@echo Offdir
is displayed as:
C:\>1.bat
Unlike the previous one, the echo off is not displayed because the @ is added, so the content behind the @ is not displayed. And because of the addition
echo off, so the following command does not appear, and the volume directly displayed in the result drive C does not have a label. The serial number of the volume is 0c5d-07ff
C:\ The directory
2004-08-25 00:45 <DIR> windows2004-08-25 00:51<dir> Documents and Settings
.....
C:\
From the above comparison, I believe you have fully mastered the echo off command.
Next is the call command:
The meaning of call is called. If there are 2 batch files A.bat and B.bat. If I want to run the A.bat running B.bat. How to run it? In fact, it is very simple, as long as the A.bat file in the input call command, you can run in the A.bat, run the B.bat, B.bat after the run, continue to execute A.bat
Call command format:
Call [Drive:][path]filename [Batch-parameters]
batch-parameters Specifies the command line information required by the batch program.
For example, we create a in the C-packing directory. BAT file, which reads:
echo This is A.bat
Call D:\b.bat
Echo Done
The B.bat is then created in the D-packing directory, which reads:
echo This is B.bat
After saving, open cmd, enter the C packing directory, and then enter 1.bat, shown below:
C:\>a.bat
C:\>echo this are A.batthis is A.bat
C:\>call D:\b.bat
C:\>echo this are B.batthis is B.bat
C:\>echo Donedone
It is easy to see from the example that the contents of the A.bat are run first, until the call B.bat is encountered, then the B.bat is called, the B.bat is run, then the return a.bat is run and then the echo-done statement followed by the calling B.bat. Until all of the batch commands for A.bat are finished running.
Note: There is a [batch-parameters] inside the reference parameter is what, know the friend can tell, appreciate.
The PAUSE command pauses execution of the batch program and displays a message prompting the user to press any key to continue execution. This command can only be used in a batch program.
REM command:
The word identifier that represents this command is interpreted as a line (note), not executed, but only for future reference (equivalent to a comment in the program).
You can also use a two colon to replace REM. :: Equal to one REM. But they have a difference, is to use:: annotation words He is not echo, even if you hit echo on forced echo is not the same. REM can also be annotated in Config.sys.
Grammar: Rem[commnet]
Batch file Parameters:
People who have a bit of programming basics know that functions have parameters. The batch file also has parameters.
I would like to make an analogy, hope to help people without the language basis can also see very clear.
Let me start with the example. First, create a batch file A in the C-packing directory. BAT, inside input content
Echo%1
Then open cmd, and then into the C-packing directory. Input: A "This is a Canshu"
The results were as follows:
C:\>a.bat "This is a test"
C:\>echo ' This is a test ' ' This is a test '
Entered in a ' this is a Canshu ', A is the new A.bat filename A (followed by. bat can be written or not), and the phrase "This is a Canshu" on the back of a is the argument, the argument written here, that the parameter is automatically placed in the batch program while the program is running. So where do you put it? Is the place where%1 is placed.
Looking at the example, let's take a look at the whole definition of the parameter:
Batch files can also use parameters (equivalent to the command line arguments of DOS commands) as functions in C, which requires a parameter notation "%". %[1-9] Represents a parameter, which is a string separated by a space (or tab) that is added after the file name when the batch file is run. Variables can represent the batch command itself from%0 to%9,%0, and other parameter strings are represented in the order of%1 to%9. We have%1 in the example of the previous program, and he is the parameter, and the "This is a test" in the input is placed directly in the position of%1 as a parameter, and the program becomes echo "This is a test".
Let me give you a few more examples to help you understand:
C: root directory The next batch of processing file name is B.bat, content is: @echo off type%1
The type is an output command in DOS that can be used to print the contents of a text file, such as creating a new 1.txt file
Enter the contents inside, save. into CMD, if the input 1.txt can not see the 1.txt file content, but if I
Want to see what to do? At this time can use the type command, as long as in the CMD input type 1.txt can be displayed
The content in the 1.txt file has the type%2 run c:\>b a.txt b.txt%1: a.txt%2: Represents b.txt
So the batch command on the above becomes a
@echo off
Type A.txt
Type B.txt the commands above will then display the contents of the A.txt and B.txt files sequentially.
People who don't have the basics of programming might ask, what do you need to get a parameter for? How troublesome is it to add a parameter to the back? Just go in there and write it right? In fact, there are also the wrong aspects of the right aspect. Let's give you an example.
The first step is to create a new batch file under the C-packing directory, which we name still a.bat. Enter the contents into:
Ping%1
The ping command can be easily understood as testing whether a machine is open or not, and if it is open, he will reply back to you. And then into the cmd, we want to test 163 of the server is not open, then enter a www.163.com, to know the ping command, you can ping to check, but if you want to ping the people do not know ping command how to use, then how to do ah? At this point you can input the command into a batch file, save it, and then let the person who will not use into CMD, run your batch file, the file name followed by his ping web site address on the line. In other words, he wants to ping 163 directly add 163 URL, want ping sina directly add Sina URL. So as long as the input of a parameter, without changing the program itself, the entire program's versatility has been greatly improved.
This is for a simple ping command, you may think that the parameters are not worth it, or directly change the good AH. But if the program has a lot of, you can not find where to change how to do ah? So, as long as the run, input parameters, the result will come out, no longer like you, consider how to write batch files. Anyone who knows what to input can make a batch program run, and the person who writes is thinking about how to get people who don't know the program to run the program.
if command
For example: If a likes B, then a will marry B. The words translated into computer language became
If a likes B A, he will marry B.
Of course, the computer can not understand a like b,a to marry B These two words, here is just an example to make it easier for you to understand.
There are 3 modes in the IF statement, as follows:
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
IF [NOT] ERRORLEVEL number command
NOT specifies that Windows XP should execute the command only if the condition is false.
ERRORLEVEL number specifies that the condition is true if the last program that is running returns an exit encoding that is equal to or greater than the specified digit.
String1==string2 if the specified text string matches, the specified condition is true.
EXIST filename If the specified filename exists, the specified condition is true.
command specifies the commands to be executed if the condition is met. If the specified condition is FALSE, the command can be followed by an else command that executes the command after the Else keyword.
Let's start with the first one:
IF [NOT] string1==string2 command
Natural statement meaning: if string1==string2, then execute command
Here's an if statement that can actually be applied.
Natural statement: If you enter a parameter of 3, then display "A=3"
Computer statement:
@echo offif "%1" = = "3" echo "A=3"
or write
@echo offif%1==3 echo "A=3"
Note: When you want to test, because under CMD, enter 1.bat 3. Because this is used to pass parameters, see the article in the previous part of the "batch file parameters."
The second type:
IF [NOT] EXIST filename command
This command term detects whether a file exists. If present, execute command. If it does not exist, then nothing is displayed.
For example: We would like to check the e-packing directory if there is a file called 2.txt. If present, the exist is displayed. If it does not exist, then nothing is displayed.
The batch processing commands are as follows:
@echo offif exist e:\2.txt echo "exist 2.txt"
The third type:
IF [NOT] ERRORLEVEL number command
This I quote point material, feel others write more detailed, the reference part is the pink word part:
if errorlevel < numbers > pending orders
Many DOS programs return a numeric value to indicate the result (or state) of the program's operation at the end of the run, using the IF ERRORLEVEL command to determine the return value of the program, and to perform different commands depending on the return value (the return value must be in order from large to small). If the return value equals the specified number, the condition is set, the command is run, or the next sentence is run.
if errorlevel 2 goto x2
= = = = = = = = = = = = = = = return value from large to small order is not required, instead, it is customary to perform commands for Goto when using set as the execution command, usually from small to large order, such as the need to put the return code into the environment variable, you need to use the following order form:
if errorlevel 1 set el=1 if errorlevel 2 set el=2 if errorlevel 3 set el=3 if errorlevel 4 set el=4 if errorlevel 5 set El =5
Of course, the following loops can be used instead, and the principle is consistent: for%%e in (1 2 3 4 5 6 7 8 ...) do if errorlevel%%e set el=%%e
Here is a For loop, which will continue to be introduced to, do not understand can jump over first
If errorlevel comparison of the return code is not equal to the judgment condition, but greater than equal. Because of Goto jump characteristics, from small to large sort will result in a small return code at the jump out; Because of the "repeat" assignment attribute of the SET command, a large to small sort causes a smaller return code to "overwrite" the larger return code.
In addition, although the IF errorlevel=< number > command is also a valid command line, it is simply command.com to interpret the command line as a command-line delimiter and ignore it

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.