In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of getting started with batch processing and improvement", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "introduction to batch processing and sample Analysis of improvement".
Do you know the default sharing? This thing is not a good thing. So delete it. But this thing is re-created every time the system is restarted. So every time you have to re-open the cmd to delete it. Extremely troublesome. But with a batch file is different, first enter the command into the batch file, and then add to the startup item, each startup will run automatically, eliminating the trouble of entering commands every time.
How do I create a batch file?
Open notepad, do not write anything, then select the file, save the type to select all files, the file name is named * .bat this * represents the file name, you can start casually. After saving, look at the place you saved, there will be an icon with a yellow gear in the white window. This is the batch file you created, double-click it and you can run it. But now he does not enter any commands, so he runs and will not do anything. when we want to add something to this * .bat file, just right-select him, and then select Edit, you can open notepad and enter commands.
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 when they are understood in depth. Batch processing is, as the name implies, a pile of things to deal with together. In other words, write a dos command into it and then execute it one by one in order. The effect is the same as when you type the dos command in cmd. To run, just double-click and you can run it. Instead of typing commands into it over and over again, this is the advantage of batch files.
In addition to running the dos command, he can also support select structure if, loop structure for,goto, etc., which is a bit similar to c, but not nearly as comprehensive as c, and the writing language is very irregular.
Batch syntax:
Let's start with the most basic thing @ echo off
Echo means rotation, here means echo, and echo off means to turn off echo. The previous @ means that the line echo off will not echo either. You can try to remove @ and the whole line. @ another function is to automatically restore the echo of the command when the batch file is executed. If you use ECHO OFF in the first sentence, the command prompt will not be displayed after the batch file is executed.
For example, if we first create a 1.bat file and enter it:
Dir is then saved under c:\. Then we run cmd, go to the root directory of disk c, type 1.bat, and display:
The volume in C:\ > dir drive C has no label. The serial number of the volume is 0C5D-07FF
Directory of C:\
2004-08-25 00:45 WINDOWS2004-08-25 00:51 Documents and Settings
.
C:\
If you modify the 1.bat content to
Echo off
Dir
Then type 1.bat in cmd, and it will display
C:\ > echo off
/ / because echo off is running, the dir command is not displayed, which directly shows that the volume in drive C has no label. The serial number of the volume is 0C5D-07FF
Directory of C:\
2004-08-25 00:45 WINDOWS2004-08-25 00:51 Documents and Settings
.
C:\
If you modify the 1.bat file to:
@ echo offdir
Is displayed as:
C:\ > 1.bat
/ / unlike before, echo off is not displayed because @ is added, so the content after @ is not displayed.
/ / echo off, so the following command does not display, but directly shows that the volume in drive C has no label. The serial number of the volume is 0C5D-07FF
Directory of C:\
2004-08-25 00:45 WINDOWS2004-08-25 00:51 Documents and Settings
.
C:\
From the above comparison, I believe you have a full grasp of the echo off command.
Next we will talk about the call command:
Call means to call. If there are two batch files a.bat and b.bat. If I want to run b.bat in the middle of a.bat. How do I run it? In fact, it is very simple. As long as you enter the call command in the a.bat file, you can run b.bat while a.bat is running, and wait for b.bat to run, then 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 the a.bat file in the root directory of disk c, which is as follows:
Echo this is a.bat
Call d:\ b.bat
Echo done
Then create a b.bat under the root directory of the d disk, which reads:
Echo this is b.bat
After saving, open cmd, go to the root directory of disk c, and then type 1.bat, which is displayed as follows:
C:\ > a.bat
C:\ > echo this is a.batthis is a.bat
C:\ > call d:\ b.bat
C:\ > echo this is b.batthis is b.bat
C:\ > echo donedone
It is easy to see from the example that you first run the contents of a.bat until you encounter call b.bat, then call b.bat, run b.bat, return to a.bat, and then run the echo done statement after call b.bat, until all the batch commands of a.bat are run.
Note: there is a [batch-parameters] what the parameters are, friends who know will be grateful if you can tell me.
The 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 a batch program.
Rem command:
Indicates that the character after this command is an interpretation line (comment), not executed, but for future reference (equivalent to comments in the program).
At the same time, you can use two colons instead of rem.:: equals a rem. But there is a difference between them, that is, if you use:: as a comment, it will not be echoed, even if you type echo on to force echo. At the same time, rem can add comments in the config.sys.
Syntax: rem [commnet]
Batch file parameters:
Anyone with some programming background 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 very well.
Let me start with the example. first, create a batch file a.bat in the root directory of disk c, and enter the contents in it
Echo 1
Then open cmd and enter the root directory of disk c. Type: a "this is a canshu"
The results are 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", an is the file name of the newly created a.bat (the following .bat can be written or not written), and the sentence "this is a canshu" after an is the parameter, and the parameter written here will automatically put the parameter into the batch program while the program is running. so where to put it? It is in the place of% 1.
Looking at the example, let's take a look at the whole definition of parameters:
Batch files can also use parameters like C language functions (equivalent to command-line arguments for DOS commands), which requires a parameter representation of "%". % [1-9] represents a parameter, which is a string separated by a space (or Tab) after the file name when running a batch file. Variables can range from% 0 to% 9jue% 0 to represent the batch command itself, and other parameter strings are represented in the order of% 1 to% 9. / / in the example of our last program, there is% 1, which is the parameter, and the input "this is a test" as a parameter is directly placed in the position of% 1, so the program becomes echo "this is a test".
Here are a few more examples to help you understand:
C: the batch processing file under the root directory is named b.bat with the content: @ echo off type% 1
/ / type is an output command in 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 inside and save. Enter cmd. If you enter 1.txt, you will not be able to see the contents of the 1.txt file, but if I
/ / what should I do if I want to see it? At this time, you can use the type command, as long as you type type 1.txt in cmd to display
/ / if the content in the 1.txt file is type% 2, then run C:\ > b a.txt b.txt% 1: means a.txt% 2: means b.txt
So the above batch command becomes
@ echo off
Type a.txt
Type b.txt then the above command will display the contents of the a.txt and b.txt files sequentially.
People who don't have a programming foundation may ask, why do you need a parameter? How troublesome is it to add a parameter to the end? Why don't you just write it in?! In fact, there are both right and wrong aspects in this way. Let's give an example to illustrate.
The first step is to create a new batch file under the root directory of disk c, which is still named a.bat. Enter the contents as follows:
Ping 1
/ / the ping command can be simply understood as testing whether a machine is on or not, and if it is turned on, he will send you a response back. Then enter cmd. If we want to test whether the server of 163is open, enter a www.163.com. For those who know the ping command, you can type ping to check, but what if those who want to ping do not know how to use the ping command? At this point, you can type the command into the batch file in advance, save it, and then let someone who doesn't know how to use it into cmd and run your batch file, followed by the address of the website he wants to ping. In other words, he wants ping 163to add 163URL and ping sina to add sina URL directly. In this way, just enter a parameter without changing the program itself. The versatility of the whole program is greatly improved.
This is a simple ping command, you may think the parameter is not worth it, or just change it. But what if there are many programs and you can't find where to change them? So, as long as you run it, enter the parameters, and the results will come out, and you don't have to think about how to write a batch file like you. People only need to know what to input to make the batch program run, while the writer is thinking about how to get people who don't understand the program to run the program.
If command
For example: if a likes b, then a will marry B. this sentence is translated into a computer language.
If if a likes b a, he will marry b.
Of course, it is impossible for the computer to understand the two sentences that a likes bjorn an and marry b. Here is just an example for you to understand.
There are three modes of 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 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, the specified condition is true.
Command specifies the command to execute 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 meaning: if string1==string2, then execute command
Here is another example of a practical if statement.
Natural statement: if the input parameter is 3, then "await 3" is displayed.
Computer statement:
@ echo offif "% 1" = "3" echo "axiom 3"
Or write it as
@ echo offif 1 room3 echo "axi3"
Note: when you want to test, because under cmd, type 1.bat 3. Because the transfer parameters are used here, take a look at the batch file parameters in the previous part of the article.
The second kind:
IF [NOT] EXIST filename command
This command term checks whether the file exists. If so, execute command. If not, nothing is displayed.
For example, we want to check whether there is a file called 2.txt in the root directory of e-disk. If it exists, it displays exist. If it does not exist, nothing is displayed.
The batch command is as follows:
@ echo offif exist e:\ 2.txt echo "exist 2.txt"
The third kind:
IF [NOT] ERRORLEVEL number command
I quote some information about this, and I feel that others have written it in more detail, and the quote part is the pink word part:
Commands to be executed by if errorlevel
Many DOS programs will return a numeric value to represent the result (or status) of the program after running. The return value of the program can be judged by the if errorlevel command, and different commands must be executed according to different return values (the return values must be arranged in the order from largest to smallest). If the return value is equal to the specified number, the condition holds, run the command, otherwise run the next sentence.
Such as if errorlevel 2 goto x2
= = Note = the order of return values from large to small is not required, but is only the idiom when the execution command is goto. When set is used as the execution command, it is usually arranged from small to large. For example, if you need to put the return code into the environment variable, you need to use 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 instead, 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, which will be described later. If you don't understand, you can skip it first.
The judgment condition of if errorlevel comparison return code is not equal to, but greater than or equal to. Because of the jump characteristic of goto, sorting from small to large will lead to jumping out at the smaller return code, while because of the "repetitive" assignment characteristic of set command, sorting from large to small will cause smaller return code to "overwrite" larger return code.
In addition, although if errorlevel= command is also a valid command line, it is just that command.com ignores = as a command line hyphen when interpreting the command line
The above is all the content of this article "getting started with batch processing and sample Analysis of improvement". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.