Give you a piece of information, which is very detailed. You can do it yourself.
How to create a batch file?
Don't feel proud after listening to the batch file. It's actually quite simple. Have you ever used a notepad? Used? All right. Open Notepad without writing anything, then select the file and save it. Select all files as the save type, and the file name will be named *. Bat stands for file name. You can take whatever you want. After saving, look at the saved place and an icon with a yellow gear will appear in the white window. This is the batch file you created. Double-click it to run it, but he hasn't entered any commands in it yet. So he won't do anything when he runs. When we want to add something to this *. Bat file, just right-click it and select Edit, then we can open Notepad and 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 will be explained later. Batch processing, as its name implies, is a lot of things piled together for processing. In other words, write a series of dos commands in it, and then execute them one by one. The effect is the same as typing dos command in cmd. After writing in batch, double-click it to run. You don't have to enter commands in it again and again. This is a batch file.
In addition to running dos commands, he can also support the selection structure if, loop structure for, goto and so on. , a bit like C, but far less comprehensive, writing language is very irregular.
Batch syntax:
Let's talk about the most basic @echo off first
Echo means cyclotron, here means echo, and echo off means turning off echo. The preceding @ indicates that offline echo will not be echoed. You can try to get rid of @ and the whole line. Another function of @ is to automatically resume the command echo after executing the batch file. If the first sentence is ECHO OFF, the command prompt will not be displayed after the batch file is executed.
For example, let's first create a 1. Bat file, we will input:
Then, the Dir is saved under c: \. Then we run cmd, enter the root directory of drive C, and enter 1.bat, and it will display:
c:\ & gt; catalogue
The volume in drive c has no label.
The serial number of this volume is 0C5D-07FF.
C:\ directory
This is a good example. DIR> window
00: 5 on August 25th, 20041< DIR> documents and settings
...........
C: \
If the contents of 1 Bats have been transformed into
echo
catalogue
Then enter 1.bat in cmd, and it will be displayed.
c:\ & gt; Echo off // Because echo off was run, the dir command was not displayed, and the result was displayed directly.
The volume in drive c has no label.
The serial number of this volume is 0C5D-07FF.
C:\ directory
This is a good example. DIR> window
00: 5 on August 25th, 20041< DIR> documents and settings
.....
C: \
If the 1.bat file is modified to:
@ Turn off echo
catalogue
It is displayed as:
c:\ & gt; 1.bat // Different from before, echo off is not displayed, because @ is added, so the content after @ is not displayed.
Echo is off, so the following commands are not displayed, and the results are displayed directly.
The volume in drive c has no label.
The serial number of this volume is 0C5D-07FF.
C:\ directory
This is a good example. DIR> window
00: 5 on August 25th, 20041< DIR> documents and settings
....
C: \
Through the above comparison, I believe you have completely mastered the echo off command. Now it's 1 ... It's so tiring to write! ! ! I took a bath and went to bed.
-
I was pulled up at 6 o'clock in the morning ... I'm unlucky. Keep writing.
-
Next is to call the command:
Call means to call, not "Oh, shit":). Calling is calling. If there are two batch files, a.bat and B.bat, I want to run B.bat in the middle of a.bat, how to run it? It's actually quite simple. As long as you enter the call command in the a.bat file, you can run b.bat while it is running, and continue to execute a.bat after it is finished.
Call command format:
Call [drive:] [path] file name [batch parameters]
Batch-parameters specifies the command line information required by the batch program.
For example, we create a. bat file in the root directory of drive C, with the following contents:
Echo This is a bat.
Call d: \ b.bat.
Echo completion
Then create b.bat in the root directory of disk D, with the following contents:
Echo, this is a bat.
Open cmd after saving, enter the root directory of drive C, and then enter 1.bat, as shown below:
c:\ & gt; bat
c:\ & gt; Echo This is a bat.
This is a bat.
c:\ & gt; Call d: \ b.bat.
c:\ & gt; Echo, this is a bat.
This is a bat.
c:\ & gt; Echo completion
finished
It is easy to see from the example that the contents of a.bat are run first, then b.bat is called, then a.bat is returned after running b.bat, and then the echo done statement after calling b.bat is run until all batch commands of a.bat are run.
Note: There is a [Batch Parameter]. I would appreciate it if my friends could tell me.
Pause command
Pauses the 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 batch programs.
Rem command:
Indicates that the character after the command is an explanation line (comment), which is not executed and is only for future reference (equivalent to a comment in the program).
At the same time, you can use two colons instead of rem. For example,:: is equivalent to a REM. But there is a difference between the two, that is, if you use:: as a comment, it will not echo, even if you type echo on to force echo. At the same time, REM can add comments in config.sys.
Syntax: rem[commnet]
Batch file parameters:
Anyone who has become a little basic knows that functions have parameters. Batch files also have parameters.
For example, I hope it can help people who have no language foundation to understand it well.
Let me give you an example first. First, create a batch file A. bat in the root directory of drive C, and input the content.
echo % 1
Then open cmd, enter the root directory of drive C, and enter: a "This is a broken book".
The results are as follows:
c:\ & gt; A.bat "This is a test"
c:\ & gt; Echo "This is a test"
"This is a test"
In the input A "This is an incomplete book", A is the file name A (the latter. Bat can be written or not), and the sentence "This is a broken book" after A is the parameter. The parameters written here will be automatically put into the batch program when the program runs. Where do you put it? Just put it at% 1
Look at this example, let's look at the complete definition of parameters:
Batch files can also use parameters (equivalent to command line parameters of DOS commands) like functions in C language, and a parameter expression "%"is required.
%[ 1-9] represents a parameter, which refers to a string separated by spaces (or tabs) added after the file name when the batch file is run. Variables can range from %0 to %9, with %0 representing the batch command itself and other parameter strings in the order of% 1 to %9. //In the example of our last program,% 1 is a parameter, and the input "This is a test" is directly placed at the position of% 1, so the program becomes echo "This is a test".
Give a few more examples to help you understand:
C: The next batch file in the root directory is named b.bat, and its contents are as follows:
@ Turn off echo
Type% 1 //type is an output command under dos, which can be used to output the contents of a text file. For example, we create a new 1.txt file.
//Enter the contents and save them. Enter cmd. If you enter 1.txt, you can't read the contents of the 1.txt file, but if I
//What if you want to see it? At this time, you can use the type command. Just enter type 1.txt in cmd and it will be displayed.
//1.txt file.
Type %2
Then run c: \ > business English
% 1: stands for a.txt
%2: stands for b.txt
So the batch command above becomes
@ Turn off echo
A.txt type
B.txt type
So the above command will display the contents of a.txt and b.txt files in turn.
People without a programming foundation may ask, why do you want to get a parameter? How troublesome is it to add a parameter after it? Why don't you just write it in? ! In fact, this method has both right and wrong aspects. Let's give an example to illustrate.
The first step is to create a new batch file in the root directory of drive C. We still call it a.bat The input content is as follows:
Ping%1/ping command can be simply understood as testing whether a machine is turned on, and if it is turned on, it will send back a response to you.
Then enter cmd. We want to test whether the server of 163 is on, so we enter a www.163.com.
For those who know the ping command, you can check it by ping, but what if the person who wants to ping doesn't know how to use the ping command? At this time, you can enter the command into the batch file in advance, save it, and then let someone who can't use it enter cmd to run your batch file, and just add the URL he wants to ping after the file name. In other words, if he wants to ping 163, he directly adds the URL of 163, and if he wants to ping Sina, he directly adds the URL of Sina. In this way, he only needs to input a parameter without changing the program itself.
This is a simple ping command. You may think it is not cost-effective to use parameters, or just change them directly. But what if there are so many programs that you can't find a place to change them at once? Therefore, whether you are a rich brother, a rich brother, a rich sister or a rich sister, as long as you run and enter the parameters, the results will come out by yourself. You don't have to think about how to write batch files like you do. People only need to know what to input to make batch programs run, while writers need to let people who don't know the programs run.
Batch parameters are as simple as that. I don't know if you understand. However, if you want to know more about batch parameters, you can continue reading. If you don't want to know more, it is enough to know so much now.
The following pink content is online information.
====================================
Because the parameter is only 1%-9%, when we want to refer to the tenth or more parameters, we must move the start pointer of the DOS parameter. The shift command plays the role of moving the pointer, moving the starting pointer of a parameter to the next parameter, similar to the pointer operation in C language. The chart is as follows:
Initial state, cmd is the command name, which can be referenced by %0.
cmd arg 1 arg 2 arg 3 arg 4 arg 5 arg 6 arg 7 arg 8 arg 9 arg 10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
& line; & line; & line; & line; & line; & line; & line; & line; & line; & line;
%0 % 1 %2 %3 %4 %5 %6 %7 %8 %9
Cmd cannot be referenced after 1 is shifted.
cmd arg 1 arg 2 arg 3 arg 4 arg 5 arg 6 arg 7 arg 8 arg 9 arg 10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
& line; & line; & line; & line; & line; & line; & line; & line; & line; & line;
%0 % 1 %2 %3 %4 %5 %6 %7 %8 %9
After two shifts, arg 1 is also discarded, and the %9 point is empty, which has no reference significance.
cmd arg 1 arg 2 arg 3 arg 4 arg 5 arg 6 arg 7 arg 8 arg 9 arg 10
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
& line; & line; & line; & line; & line; & line; & line; & line; & line; & line;
%0 % 1 %2 %3 %4 %5 %6 %7 %8 %9
Unfortunately, win9x and DOS do not support the reverse operation of shift. Only in the nt kernel command line environment, shift supports the /n parameter, and the start pointer can be moved back based on the first parameter.
=================
If go to advanced syntax options
Let's call it a day. I'm going to pack up and go back to school. Maybe if goto chooses advanced grammar, it will be finished in Shenyang. Wish me luck.
======================
The school's "Internet cafe" has finally reopened, so finish the rest quickly.
If command
To put it bluntly, if is equivalent to if in our vernacular.
For example, if A likes B, then A will marry B and just translate this sentence into computer language.
If a likes b a, she must marry b.
Of course, the computer can't understand the two sentences that A likes B and A will marry B. This is just for your convenience.
If statement I * * * has three modes, as follows:
IF [NOT] string 1==string2 command
If the file name command does not exist
IF [NOT] ERRORLEVEL number command
NOT specifies that Windows XP should execute this command only if the condition is false.
ERRORLEVEL number If the exit code returned by the last running program is equal to or greater than the specified number, the specified condition is true.
String 1==string2 If the specified text strings match, the specified condition is true.
If the specified file name exists, the specified condition is true.
Command If the condition is met, specify the command to be executed. If the specified condition is FALSE, the command can be followed by an ELSE command, which is executed after the ELSE keyword.
Explain the first one first:
IF [NOT] string 1==string2 command
Natural sentence meaning: if String 1 = = String2, then execute the command.
Here is another if statement that can be used in practice.
Natural statement: If the input parameter is 3, "A = 3" will be displayed.
Computer statement:
@ Turn off echo
If "%1"= "3" echoes "a=3"
Or write
@ Turn off echo
If% 1==3, "a=3" is echoed.
Note: When testing, because under cmd, enter1.bat3. Because pass-through parameters are used here, see "Batch file parameters" at the front of the article for details.
The second type:
If the file name command does not exist
The command language detects whether a file exists. If it exists, please execute the command. If it does not exist, nothing will be displayed.
For example, we want to check whether there is a file named 2.txt in the root directory of E disk. If it EXISTs, it will display exist. If it does not exist, nothing will be displayed.
The batch command is as follows:
@ Turn off echo
If e:\2.txt exists, echo "2.txt exists"
The third kind:
IF [NOT] ERRORLEVEL number command
I quote some information about this, and I feel that others have written it in detail. The quotation part is pink:
If errorlevel & lt number > command to be executed.
Many DOS programs will return a digital value to represent the result (or state) after the program runs. You can judge the return value of the program through the if errorlevel command, and execute different commands according to different return values (the return values must be arranged in descending order). If the return value is equal to the specified number, the condition holds, run the command, otherwise run the next sentence.
For example, if error level 2 goes to x2.
= = = = Note = = = = = = = =
The order of return values from large to small is not necessary, but the idiom when executing the command is goto. When set is used as an execution command, it is generally arranged from small to large. For example, to put the return code in an environment variable, you need to use the following order:
If the error level is 1, set el= 1
If error level 2 sets el=2
If error level 3 is set to el=3
If the error level is 4, el=4 is set.
If the error level is 5, set el=5.
...
Of course, the following cycle can also be used instead, and the principle is the same:
For%% e in (1234 5677 8 ...) do if error level%% e set el =%% e//This is a for loop, which will be introduced later. If you don't understand, you can skip it first.
If errorlevel compares the return codes, the judgment condition is not equal to, but greater than or equal to. Because of the jumping characteristics of goto, sorting from small to large will lead to jumping out at the smaller return code; However, due to the "repeated" assignment feature of the set command, sorting from large to small will cause the smaller return code to "overwrite" the larger return code.
In addition, although if error level = Command is also a valid command line, it is only that command.com ignored = as a command line separator when interpreting the command line.
Select 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. Through the for command, we can control the number of loops and so on.
Grammar:
Execute command [command-parameter] on% variable in (set)
%variable specifies a one-letter replaceable parameter.
(set) Specifies a file or a set of files. Wildcards can be used.
Command specifies the command to execute on each file.
command parameter
Specify parameters or command-line switches for a specific command.
When using the FOR command in a batch file, use %%variable to specify a variable.
Not the% variable. Variable names are case-sensitive, so %i is different from% i.
I don't know if you understand it, but it's actually very easy to understand. Let's give an example. I want to print all bat files and txt files in the root directory of disk C with type. Under the command. Dos is of type *. Bat *. txt。 First, save the file in the root directory of drive C, and the file name is a.bat
Use the for command as follows:
For %%t in (*. Bat *. Txt) do type %%t
%%t actually represents a parameter, and its contents are in parentheses in in (). In other words, %%t in this sentence becomes *. Bat *.txt. do means to do, and the type is followed by %%t, and %%t is *. Bat *.txt. So the original intention of this command becomes
Type *. Bat *. Textfile (textfile)
When executing, enter cmd, and then enter a.bat in the root directory of drive C to print out all the file contents with the extension. Bats and. Txt is in the root directory of drive C.
Note here: there is a space after in.
The for command extension is used under xp, so the function of for becomes more powerful. Let's talk about a real cycle.
FOR/L% variable IN (start, step, end) DO command [command-parameter]
This set represents a series of numbers that increase from beginning to end.
Therefore, (1, 1, 5) will generate the sequence 1 2 3 4 5, (5, 1, 1) will generate.
Sequence (5 4 3 2 1)
The first 1 is at the starting position, indicating the starting position, and the second 1 is at the stepping position, indicating stepping in English. In this case, it means that each increment is 1. The following 5 is at the end position, indicating the size of the end.
This sentence means increasing from 1 (starting) and increasing 1 (stepping) at a time until 5 (ending).
What's the use? In fact, I feel that this thing is still very useful. For the simplest example, we have to repeat the sentence "I am the best" and display 10 times. Then the for command is as follows:
Echo "I am the best" for /L %%e in (1, 1 0).
At this point, cmd will repeatedly enter "I am the best" 10 times.
===============================================
Have you read the whole article? Good ... writing is not easy for me. ....
Now I don't know what you know about bat. My feeling now is that bat is a combination of dos commands. You write all dos commands as bat commands. As long as you run bat, you will execute dos commands one by one, which undoubtedly provides a lot of convenience.
Let me give you a few more examples.
Delete the default * * * enjoyment:
I don't know how much you know about the default * * *, but this is a hidden danger. The only way now seems to be to delete a bat file. The command is as follows:
Net share IPC $/ delete
Net share management fee/deletion
Net share c USD/deleted
Net share d $/ deleted
Net share e USD/deleted
……
The c d e in it is your drive letter. If there is only one partition, write net share c$ /delete. If you have n partitions, write them down one by one.
Net share d $/ deleted
Net share e USD/deleted
Net share f USD/deleted
Net share g USD/delete ...
Shortcut to log on to the local area network (the other machine has a password, which is a system above 2000).
Net use \ \192.168.0.1[password]/user: [user name]
Browser \ \192.168.0.1
Bat file backup registry
Set regfile=%date% // Set the variable, and the following %regfile% will automatically replace "today's date".
Go to the end if "%regfile%" exists//If a directory named after today's date is found, skip to the end of the file.
Md Temporary//Create a temporary directory
Call 1.bat // Call 1.bat.
Del 1.bat
Ren 1.bat
Ren 3. Bat 2. Bat
let
Send back the mobile "%regfile%" temp >; 4.bat // Write a bat file and move the "directory named by date" to temp.
Md "%regfile%" // Create a directory named by date on the same day.
CD“% regfile %”//
Reg export hkcu hkcu.reg // Export registry
Export hklm hklm.reg
//HKEY _ Current _ User Abbreviation hkcu. Store the personal data of the current user.
//HKEY_LOCAL_MACHINE abbreviation hklm. Core data of the system
laser record ..
del tree/y temp & gt; Nul/// Return to the parent directory and delete the temp folder.
: end