Bat batch file

Source: Internet
Author: User

How to Create a batch file?

Don't listen to the batch processing file. It's actually very simple. Have you ever used notepad? Used? Okay. open notepad without writing anything, select a file, and save it. select all files for the SAVE type, and name the file name *. bat * indicates the file name. You can start it as needed. after saving it, you can see where you saved it. A yellow gear icon appears in the white window. this is the batch file you created. You can double-click it to run it. However, since no command is entered in it, it does not do anything. when we want to move to this *. when adding an item to the BAT file, right-click it and select Edit. Then, you can open notepad and enter the command in it.

  What are the commands in the batch file?

For the time being, the commands in the batch file can be understood as the DOS command, and will be explained later. batch Processing, as its name implies, is a pile of things heap together for processing. in other words, you can write a DOS command to it and execute it in sequence. The effect is similar to that of doscommand in cmd. after writing it in batches, you only need to double-click it to run it. instead of repeatedly executing commands in it. this is the benefit of batch processing files.

In addition to running the doscommand, it also supports selecting the structure if, loop structure for, Goto, and so on. It is a bit similar to C, but far from comprehensive C, and the programming language is very nonstandard.

  Batch Processing Syntax:

First, let's talk about the most basic things.@ Echo off

Echo refers to the loop. Echo off refers to the echo function. the previous @ indicates that the echo off line does not show back. You can try to remove @ and the whole line. another function of @ is to automatically restore the command echo when the batch processing file is executed. If echo off is used in the first sentence, the command prompt is not displayed after the batch file is executed.

For example, if we create a 1. BAT file first, enter:

DIR is then saved in c: \. Then run cmd, enter the C root directory, and enter 1.bat. then, it is displayed:

C:> dir
The volume in drive C is not labeled.
The serial number of the volume is 0c5d-07ff.

C: Directory

<Dir> Windows
<Dir> Documents and Settings

...........

C:

If you change the content of 1. BAT

Echo off

Dir

Then enter 1.batin cmd, and then it will be displayed

 

C:> echo off // because echo off is run, the Dir command is not displayed and the result is displayed directly.
The volume in drive C is not labeled.
The serial number of the volume is 0c5d-07ff.

C: Directory

<Dir> Windows
<Dir> Documents and Settings

.....

C:

If you change the 1. BAT file:

@ Echo off
Dir

Displayed:

C:> 1.bat // unlike the previous one, echo off is not displayed because @ is added, so the content following @ is not displayed.

// The ECHO is off, so the subsequent command is not displayed and the result is displayed directly.
The volume in drive C is not labeled.
The serial number of the volume is 0c5d-07ff.

C: Directory

<Dir> Windows
<Dir> Documents and Settings

....

C:

 

Next we will talk aboutCall Command:

Call is a call called, rather than a "Oh rely on :). call indicates a call. assume there are two batch processing files. BAT and B. BAT. if I want to run. run B in bat. BAT. how to run it? In fact, it is very simple, as long as. enter the call command in the BAT file. in bat running, run B. BAT, and so on B. after the bat operation is complete, execute. bat

Call Command Format:

Call [drive:] [path] filename [batch-parameters]

Batch-parameters specifies the command line information required by the batch processing program.

For example, we create the. BAT file in the C root directory. The content is:

Echo this is A. bat

Call D: B. bat

Echo done

Create B. bat in the root directory of drive D. The content is:

Echo This is B. bat

After saving, open cmd, enter the C root directory, and enter 1.bat, as shown below:

C:> A. bat

C:> echo this is A. bat
This is a. bat

C:> call D: B. bat

C:> echo this is B. bat
This is B. bat

C:> echo done
Done

It is easy to see from the example that A is run first. bat content until call B is encountered. after bat, call B. bat, run B. after bat, return. bat then runs call B. echo done statement after bat until. all batchcompute commands of BAT have been run.

Note: There is a parameter in [batch-parameters]. I am very grateful if you can tell me the parameter.

Pause command

Pause the execution of the batch processing program and display a message prompting you to press any key to continue execution. You can only use this command in a batch processing program.

Rem command:

It indicates that the character after this command is interpreted as a line (comment). If it is not executed, it is only used for future reference (equivalent to a comment in the program ).

At the same time, you can use two colons to replace REM. for example: equivalent to a rem. but there is a difference between them: for comments, he does not echo, even if you play echo on to force echo, it is not the same. at the same time, REM can be used in config. comment in SYS.

Syntax: REM [commnet]

 

 

Batch File parameters:

People who have some programming basics know that functions have parameters. batch files also have parameters.

For example, I want to help people without a language base understand it very well.

Let me start from the example. First, create a batch processing file a. bat under the C-drive root directory, and enter the content

Echo % 1

Open CMD and enter the C root directory. Input: A "This Is A canshu"

The result is as follows:

C:> A. Bat "this is a test"

C:> echo "this is a test"
"This is a test"

In the input a "This Is A canshu", a is the new. BAT file name A (. BAT can be written or not), and the "This Is A canshu" statement after a is a parameter, when the program is running, the parameter is automatically placed in the batch processing program. so where? Put it at % 1.

After reading the example, let's see how the Parameter definition is:

The batch file can also use parameters (equivalent to the command line parameters of the doscommand) like C functions, which requires a parameter identifier "% ".
% [1-9] indicates a parameter. A parameter is a string separated by spaces (or tabs) after the file name when a batch file is run. Variables can be changed from % 0 to % 9.% 0 indicates the Batch Processing Command itself. Other parameter strings are represented in the order of % 1 to % 9. // In the previous program example, % 1 is the parameter, and the input "this is a test" is directly placed at % 1 as the parameter, so the program becomes echo "this is a test ".

Here are a few examples to help you understand:

C: The name of the processing file under the root directory is B. bat, and the content is:
@ Echo off
Type % 1 // type is the output command in the DOS, which can be used to output the content of the hosts file. For example, we create a new 1.txt file.

// Enter the content in the text box to save the content. If 1.txt is input, the content of the 1.txt file cannot be viewed.

// What should I do? At this time, you can use the TYPE Command, as long as you enter type 1.txt in cmd to display

// 1.txt File Content
Type % 2

Run C:> B a.txt B .txt.
% 1: paia.txt
% 2: Invalid B .txt

The above Batch Processing Command becomes

@ Echo off

Type a.txt

Type B .txt
The following command displays the.txt and B .txt files in sequence.

People without a programming foundation may ask, what do they need to get a parameter? How troublesome is it to add a parameter after? Just write it directly into it ?! In fact, there are also some mistakes in this case. Let's take an example to illustrate it.

The first step is to create a batch processing file under the C root directory. We still name it A. bat. Enter the following content to the file:

Ping % 1 // The ping command can be simply understood as testing whether a machine is on or not. If it is on, it returns a response to you.

Then, run cmd. If we want to test whether the 163 server is on, enter a www.163.com.

For those who know the ping command, you can ping the command for check. But if the target Ping user does not know how to use the ping command, what should they do? At this time, you can pre-input the command to the batch processing file, save it, and then let unused people into cmd, run your batch processing file, add the address of the website to be pinged after the file name. in other words, if he wants to ping 163, he will directly add the 163 URL. If he wants to ping Sina, he will directly add the Sina URL. in this way, as long as you enter a parameter without modifying the program itself, the universality of the entire program is greatly improved.

 

This is a simple ping command. You may think it is not worthwhile to directly modify the parameters. But if there are many programs, what should you do if you cannot find them? Therefore, no matter if you are a food elder brother, a food younger brother, a food elder sister, or a vegetable younger sister, you only need to run the command, enter the parameters, and the results will come out on your own, consider how to compile a batch file. people only need to know what is input to allow the batch processing program to run, while those who write are thinking about how to enable people who do not understand the program to run the program.

Batch Processing Parameters are that simple. Do you understand them? But if you want to know more about the batch processing parameters, you can continue to look at it. If you don't want to know more about it, it's enough to know it now.

The following pink content is online information.

==========================================

Because the parameter is only 1%-9%, but when we want to reference the tenth or more parameters, we must move the start pointer of the DOS parameter. the shift command is acting as the pointer to move the starting pointer of the parameter to the next parameter, similar to pointer operations in C. the figure is as follows:

Initial status. CMD is the command name and can be referenced with % 0.
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^
& Line;
% 0% 1% 2% 3% 4% 5% 6% 7% 9

After one shift, CMD cannot be referenced.
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^
& Line;
% 0% 1% 2% 3% 4% 5% 6% 7% 9

After two shifts, arg1 is also deprecated, and % 9 points to null, with no reference significance.
CMD arg1 arg2 arg3 arg4 arg5 arg6 arg7 Arg8 arg9 arg10
^
& Line;
% 0% 1% 2% 3% 4% 5% 6% 7% 9

Unfortunately, both Win9x and DOS do not support shift inverse operations. shift supports the/n parameter only in the NT kernel command line environment. You can use the first parameter as the benchmark to move the start pointer.
========================

 

If command

To put it bluntly, if is equivalent to if in our vernacular.

For example, if a prefers B, A will marry B.

If a like B a, it will marry B.

Of course, it is impossible for a computer to understand the two sentences that a prefers B. Here is just an example for your convenience.

If statements have three modes:

If [not] string1 = string2 command
If [not] exist filename command
If [not] errorlevel number command

Not indicates that Windows XP should execute this command only when the condition is false.

Errorlevel number if the last running program returns an exit code equal to or greater than the specified number, the specified condition is true.

String1 = string2 if the specified text string matches, the specified condition is true.

Exist filename: if the specified file name exists, set the condition to true.

If the command meets the conditions, specify the command to be executed. If the specified condition is false, the command can be followed by an else command that executes the else keyword.

First, let's explain the first one:

If [not] string1 = string2 command

If string1 = string2, Execute Command

Next we will give you an if statement that can be applied in practice.

Natural statement: If the input parameter is 3, "A = 3" is displayed"

Computer statement:

@ Echo off
If "% 1" = "3" Echo "A = 3"

Or write it

@ Echo off
If % 1 = 3 Echo "A = 3"

Note: When testing, enter 1.bat 3 under cmd, because the passing parameters are used here. For details, see "batch file Parameters" in the previous section ".

Second:

If [not] exist filename command

This command is used to check whether a file exists. If yes, Run Command. If no, nothing is displayed.

For example, we want to check that the eroot directory contains a file named 2.txt. If yes, exist is displayed. If no, nothing is displayed.

The batch processing command is as follows:

@ Echo off
If exist E: 2.txt echo "exist 2.txt"

Third:

If [not] errorlevel number command

Here I reference some materials, and I feel that others have written more details. The reference part is the pink part:

If errorlevel <number> command to be executed

Many DOS Programs return a numeric value to indicate the result (or status) of the program running after the execution ends. The if errorlevel command can be used to determine the return value of the program, different commands are executed based on different return values (the return values must be sorted in ascending order ). If the return value 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

===== Note ==============
It is not necessary to sort the returned values in ascending order, but it is a customary usage when the command is executed as Goto. When the SET command is used as the command to execute, it is usually arranged in ascending order, for example, you need to place the return code in the environment variable in the following order:

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, you can also use the following loop to replace it. The principle is the same:
For % E in (1 2 3 4 5 6 7 8 ...) do if errorlevel % E set El = % E // here is a for loop. We will continue to introduce it later. If you cannot understand it, you can skip it first.

If errorlevel comparison return code judgment condition is not equal to, but greater than or equal. due to the redirection feature of Goto, sorting from small to large will cause a jump out of a small return code. Due to the "repeated" value assignment feature of the set command, sorting from large to small causes a smaller return code to overwrite a larger return code.

In addition, although if errorlevel = <number> command is also a valid command line, command.com only interprets the command line as a command line splitter and ignores it.

Choice command

????

Goto command

For command

The for command is actually a loop command. If we want to repeat a statement, we can use the for command to control the number of loops.

Syntax:

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

% 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
Specify parameters or command line switches for specific commands.

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.

I don't know if you understand it. It's actually easy to understand. Let's give an example. I want to use type to print BAT files and TXT files under all c-drive root directories. the command under DOS is type *. bat *. TXT. first, save the file in the C root directory named. bat

Use the for command as follows:

For % t in (*. bat *. txt) do type % t

% T is actually a parameter. Its content is in the brackets in. that is to say, % t in this sentence is changed *. bat *. TXT. do is to execute the type command, and the type is followed by % T, and % T is *. bat *. TXT. so the original meaning of this command is changed:

Type *. bat *. txt

Enter a.bat.txt in the C root directory.

Note: there is a space behind in.

 

Under XP, The for command extension is used, so the for function becomes more powerful. The following describes a real loop.

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

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)

The first 1 is at the start position, which indicates the start position, and the third 1 is at the step position. The English meaning is a cross-step, where the meaning of each increment is 1. the position of the 5 behind the end indicates the size of the end.

This statement increases from 1 (start) to 5 (end.

What is the purpose of this? In fact, I think this thing is still very useful. for the simplest example, we want to repeat the "I am the best" Statement and repeat it 10 times. the for command is as follows:

For/L % E in (1, 1, 10) Do echo "I am the best"

Then, CMD will repeat "I am the best" 10 times.

========================================================== ========

Have you read the entire article? Cough... it's not easy for me to write ....

I don't know how you understand bat. I feel like bat is a combination of doscommands. You write all the doscommands into the bat command, as long as you run bat, the doscommand will be executed one by one, which undoubtedly provides a lot of convenience.

Next I will give you some examples.

Delete default share:

I don't know how much you know about default sharing. It is a hidden danger to keep it. Now the only way is to delete a BAT file. The command is as follows:

NET Share IPC $/delete
NET Share ADMIN $/delete
NET Share C $/delete
NET Share d $/delete
NET Share e $/delete

......

The c d e in it is your drive letter. If you only have one partition, you can write it to net share C $/Delete. If you have n partitions, write them one by one.

NET Share d $/delete
NET Share e $/delete

NET Share F $/delete

NET Share G $/delete ......

Machine shortcut for logging on to the LAN (the other machine has a password and is 2000 or above)

Net use/192.168.0.1 [Password]/User: [user name]
Explorer/192.168.0.1

BAT file backup Registry

Set regfile = % date % // set the variable. The values of % regfile % are automatically replaced with "date of the current day"

If exist "% regfile %" Goto end // if you find the directory named by date on the current day, It will jump to the end of the file.
MD temp // create the temp directory

Call 1.bat // call 1.bat
Del 1.bat
Ren 2.bat 1.bat
Ren 3.bat 2.bat
Ren 4.bat 3.bat
Echo move "% regfile %" Temp> 4.bat // write and move "directory named by date of the current day" to the BAT file of temp.

MD "% regfile %" // create the directory named after date on the current day
Cd "% regfile %" // enter

Reg export hkcu. Reg // export the Registry
Reg export HKLM. Reg
// The abbreviation of HKEY_CURRENT_USER is hkcu. Store personal data of current user
// The abbreviation of HKEY_LOCAL_MACHINE is HKLM. Core System Data
CD ..
Deltree/y temp> NUL // return to the parent directory and delete the temp folder
: End

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.