![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-11 07:48 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » Batch processing learning materials (it is recommended that novices learn about it) |
| Printable Version 7,514 / 12 |
| Floor1 bagpipe | Posted 2006-08-04 11:28 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
I haven't been to this section for a long time. It seems there are no good posts, always a few posts going around. I hope newbies will work hard to post and gain experience and skills in practice...
How to create a batch file? Don't feel awesome when you hear about batch files. Actually, this thing is very simple. Have you used Notepad? Yes? Good. Open Notepad, don't write anything, then choose File, Save. Select All Files for the save type, and name the file *.*bat. The * here represents the file name, you can name it casually. After saving, look at the place where you saved it, and there will be a white window with a yellow gear icon. This thing is the batch file you created. Double-click it to run, but since there is no command input in it now, it won't do anything when it runs. When we want to add something to this *.*bat file, just right-click on it, then choose Edit, and you can open Notepad to enter commands. What are the commands in the batch file? The commands in the batch file can be temporarily understood as DOS commands, and they will be explained later when you understand them in depth. Batch processing means dealing with a large number of things together. In other words, write DOS commands one by one, and execute them in sequence. The effect is the same as typing DOS commands in cmd. The only difference is that after writing the batch file, you can run it by double-clicking it instead of repeatedly typing commands in it. This is the advantage of the batch file. In addition to running DOS commands, it can also support the selection structure if, the loop structure for, goto, etc., which is similar to C, but far less comprehensive than C, and the programming language is very non-standard. Batch processing syntax: First talk about the most basic thing @echo off The meaning of echo is echo. Here it means echoing. echo off means turning off echoing. The @ in front means that this line of echo off will not be echoed either. You can try to remove @ and the whole line. Another function of @ is to automatically restore command echoing when the batch file is executed. If the first sentence uses ECHO OFF, then the command prompt will not be displayed after the batch file is executed. For example: If we first create a 1.bat file, enter: dir and save it in c:\. Then we run cmd, enter the root directory of the c drive, and enter 1.bat, then it will display: C:\>dir Volume in drive C has no label. Volume Serial Number is 0C5D-07FF Directory of C:\ 2004-08-25 00:45 <DIR> WINDOWS 2004-08-25 00:51 <DIR> Documents and Settings ........... C:\ If the content of 1.bat is modified to echo off dir Then enter 1.bat in cmd again, it will display C:\>echo off //Because echo off is run, the dir command is not displayed, and the result is directly displayed Volume in drive C has no label. Volume Serial Number is 0C5D-07FF Directory of C:\ 2004-08-25 00:45 <DIR> WINDOWS 2004-08-25 00:51 <DIR> Documents and Settings ..... C:\ If the 1.bat file is modified to: @echo off dir Then it will be displayed as: C:\>1.bat //Different from the previous one, echo off is not displayed. The reason is that @ is added, so the content after @ is not displayed. Also, because echo off is added, the subsequent commands are not displayed, and the result is directly displayed Volume in drive C has no label. Volume Serial Number is 0C5D-07FF Directory of C:\ 2004-08-25 00:45 <DIR> WINDOWS 2004-08-25 00:51 <DIR> Documents and Settings .... C:\ Through the above comparison, I believe you have fully grasped the echo off command. Now it's 1 o'clock... It's tiring to write!!! Going to take a shower and sleep ---------------------- Got up at 6 o'clock in the morning... I'm so miserable, then continue writing ---------------------- Next, talk about the call command: call is the call of call, not "wo kao" of kao:) The meaning of call is to call. Suppose there are two batch files a.bat and b.bat. If I want to run b.bat in the middle of running a.bat. How to run it? Actually, it's very simple. Just enter the call command in the a.bat file, then you can run b.bat in the middle of running a.bat, and after b.bat runs, continue to execute a.bat Call command format: CALL filename batch-parameters Specify the command line information required by the batch program. For example, we create a.bat file in the root directory of the c drive, and the content is: echo this is a.bat call d:\b.bat echo done Then create b.bat in the root directory of the d drive, and the content is: echo this is b.bat After saving, open cmd, enter the root directory of the c drive, then enter 1.bat, and the display is as follows: 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 it first runs the content of a.bat, and until it encounters call b.bat, it calls b.bat. After b.bat runs, it returns to a.bat and continues to run the echo done statement after call b.bat until all the batch commands of a.bat are run. Note: The mentioned here, what does the parameter refer to? Friends who know, can you inform, thank you very much. PAUSE command Pause the execution of the batch program and display a message prompting the user to press any key to continue execution. This command can only be used in batch programs. rem command: It means that the characters after this command are explanation lines (comments), which are not executed, just for your future reference (equivalent to comments in the program). At the same time, you can use two colons to replace rem. For example: :: is equivalent to a rem. But they have a difference. If you use :: as a comment, it will not be echoed. Even if you type echo on to force echoing, there is no same. At the same time, rem can be used to add comments in config.sys. Syntax: rem Batch file parameters: People who are a bit more basic know that functions have parameters. Batch files also have parameters. I'll give an example to help people who don't have a language foundation understand very clearly. First create a batch file a.bat in the root directory of the c drive, and enter the content echo %1 Then open cmd, and enter the root directory of the c drive. Enter: 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 file name a of the newly created a.bat (the .bat at the back can be written or not written), and the sentence "this is a canshu" after a is the parameter. The parameter written here will be automatically put into the batch program during program operation. Then where is it placed? It is placed at the %1 position. After looking at the example, let's see how the entire definition about parameters is: Batch files can also use parameters like C language functions (equivalent to command line parameters of DOS commands), which requires the use of a parameter identifier "%". % represents a parameter, and the parameter refers to the string separated by spaces (or Tab) after the file name when running the batch file. Variables can be from %0 to %9, %0 represents the batch command itself, and other parameter strings are represented in order by %1 to %9. // In the example of our previous program, there is %1, which is the parameter, and the input "this is a test" is used as the parameter and directly placed in the %1 position, so the program becomes echo "this is a test". Let's give a few more examples to help you understand: A batch file named b.bat in the root directory of C: has the content: @echo off type %1 //type is an output command in DOS, which can be used to output the content of a text file. For example, we create a 1.txt file //Enter content inside, save. Enter cmd, if you enter 1.txt, you can't see the content of the 1.txt file, but if I //How to see it? At this time, you can use the type command. As long as you enter type 1.txt in cmd, you can display //The content in the 1.txt file type %2 Then run C:\>b a.txt b.txt %1 : represents a.txt %2 : represents b.txt Then the above batch command becomes @echo off type a.txt type b.txt Then the above command will sequentially display the contents of the a.txt and b.txt files. People without programming foundation may ask, why bother with parameters? It's so troublesome to add a parameter later? Just write it in directly? In fact, this idea is both right and wrong. Let's give an example to illustrate. The first step is to create a batch file in the root directory of the c drive, and we still name it a.bat. Enter the content: ping %1 //The ping command can be simply understood as testing whether a machine is on or not. If it is on, it will send back a response. Then enter cmd, we want to test whether the server of 163 is on, then enter a www.163.com For people who know the ping command, you can type ping to check, but if the person who wants to ping doesn't know how to use the ping command, what should I do? At this time, you can pre-enter the command into the batch file, save it, and then let the person who doesn't know how to use it enter cmd, run your batch file, and add the website address he wants to ping after the file name. In other words, if he wants to ping 163, he can directly add the 163 website address, and if he wants to ping sina, he can directly add the sina website address. In this way, as long as you enter a parameter, you don't need to modify the program itself, and the versatility of the entire program is greatly improved. This is for a simple ping command. You may think that using parameters is not worth it, and you can just modify it directly. But if there are many programs, what if you can't find where to modify it? Therefore, no matter whether you are a vegetable brother, vegetable brother, vegetable sister, or vegetable sister, as long as you run it and enter the parameters, the result will come out by itself, and you don't need to think about how to write the batch file like you. The person who writes it just needs to think about how to let the person who doesn't understand the program run the program. Batch processing parameters are so simple. I don't know if you understand it. But if you want to understand batch processing parameters in depth, you can continue to read down. If you don't want to go deep, knowing so much now is enough. The following pink content is online information. ==================================== Because there are only 1% - 9% parameters, but when we want to reference the tenth or more parameters, we must move the parameter start pointer of DOS. The shift command acts as this moving pointer. It moves the parameter start pointer to the next parameter, similar to the pointer operation in C language. The diagram is as follows: Initial state, cmd is the command name, which can be referenced by %0 cmd arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ &line; &line; &line; &line; &line; &line; &line; &line; &line; &line; %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 After 1 shift, cmd cannot be referenced cmd arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ &line; &line; &line; &line; &line; &line; &line; &line; &line; &line; %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 After 2 shifts, arg1 is also discarded, and %9 points to empty, which has no reference meaning cmd arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ &line; &line; &line; &line; &line; &line; &line; &line; &line; &line; %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 Unfortunately, neither win9x nor DOS supports the reverse operation of shift. Only in the nt kernel command line environment does shift support the /n parameter, which can repeatedly move the start pointer with the first parameter as the benchmark. ================= if goto choice for advanced grammar Let's stop writing today. I'm packing up and getting ready to go back to school. Maybe the advanced grammar of if goto choice for will be completed in Shenyang. Wish me good luck. ====================== The "internet café" in the school has finally reopened. Hurry up and finish the rest if command To put it plainly, if is equivalent to "if" in our vernacular. For example: If a likes b, then a should marry b. This sentence is translated into computer language as if a likes b a should marry b. Of course, the computer can't understand the two sentences a likes b and a should marry b. Here it's just an example to make it easier for you to understand. There are 3 modes of the if statement, as follows: IF string1==string2 command IF EXIST filename command IF 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 run program returns an exit code equal to or greater than the specified number. string1==string2 Specifies that the condition is true if the specified text string matches. EXIST filename Specifies that the condition is true if the specified file name exists. command Specifies the command to execute if the condition is met. If the specified condition is FALSE, an ELSE command that executes the command after the ELSE keyword can be followed after the command. We first explain the first one: IF string1==string2 command Naturally, the meaning of the sentence: If string1 == string2, then execute command Here's another practical if statement. Natural sentence: If the input parameter is 3, then display "a=3" Computer sentence: @echo off if "%1"=="3" echo "a=3" Or write it as @echo off if %1==3 echo "a=3" Note: When testing, because in cmd, enter 1.bat 3. Because here parameters are passed, specifically see the previous part of the article "Batch file parameters". The second type: IF EXIST filename command This command is used to detect whether the file exists. If it exists, execute command. If it doesn't exist, nothing is displayed. For example: We want to check whether there is a file named 2.txt in the root directory of drive e. If it exists, display exist. If it doesn't exist, nothing is displayed. The batch command is as follows: @echo off if exist e:\2.txt echo "exist 2.txt" The third type: IF ERRORLEVEL number command I will quote some materials. I feel that others have written more detailed. The quoted part is in pink: if errorlevel <number> command to be executed Many DOS programs return a digital value to indicate the running result (or status) after running. Through the if errorlevel command, you can judge the return value of the program, and decide to 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 is established, run the command, otherwise run the next sentence. Such as if errorlevel 2 goto x2 ==== Note =========== The descending order arrangement of return values is not necessary, but it is only a customary usage when the execution command is goto. When using set as the execution command, it is usually arranged in ascending order. For example, if you need to put the return code into an 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, 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, which will be introduced later. If you don't understand, you can skip it first The judgment condition of if errorlevel = <number> command is not equal to, but greater than or equal to. Due to the jump characteristic of goto, sorting from small to large will cause jumping at a smaller return code; and due to the "repeated" assignment characteristic of set command, sorting from large to small will cause a smaller return code to "override" a larger return code. In addition, although if errorlevel=<number> command is also a valid command line, it is just that command.com ignores the = as a command line delimiter when interpreting the command line. 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. Through the for command, we can control the number of loops, etc. Syntax: FOR %variable IN (set) DO command %variable Specifies a single letter replaceable parameter. (set) Specifies one or a group of files. Wildcards can be used. command Specifies the command to be executed for each file. command-parameters Specifies parameters or command line switches for a specific command. When using the FOR command in a batch file, use %%variable instead of %variable to specify the variable. Variable names are case-sensitive, so %i is different from %I. I don't know if you understand it. Actually, it's still easy to understand. Let's give an example. I want to print all bat files and txt files in the root directory of the c drive using type. The command under DOS is type *.bat *.txt. First, save this file in the root directory of the c drive, named a.bat Use the for command as follows: for %%t in (*.bat *.txt) do type %%t %%t actually represents a parameter, and its content is in the content in the in() parentheses. That is to say, %%t in this sentence becomes *.bat *.txt. do means to do, execute the type command, and the type is followed by %%t, and %%t is *.bat *.txt. So the meaning of this sentence becomes: type *.bat *.txt When executing, enter cmd, then enter a.bat in the root directory of the c drive, and you can print the contents of all files with extensions .bat and .txt in the root directory of the c drive. Here you need to pay attention: there is a space after in. In xp, the for command extension is enabled, so the function of for becomes more powerful. Let's talk about a real loop. FOR /L %variable IN (start,step,end) DO command This set represents a digital sequence from start to end in incremental form. 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 in this sentence is in the start position, meaning the starting position, the second 1 is in the step position, the English meaning is step, and here it means that each increment is 1. The 5 at the end is in the end position, meaning the size at the end. The meaning of this sentence is to start from 1 (start), increase by 1 each time (step), and keep changing until it becomes 5 (end). What's the use of this? Actually, I feel that the use of this thing is still very great. Let's give the simplest example. We want to repeatedly echo the sentence "i am the best" 10 times. Then the for command is as follows: for /L %%e in (1,1,10) do echo "i am the best" At this time, cmd will repeatedly enter "i am the best" 10 times. =============================================== Have you read the whole article? Hey... It's not easy for me to write.... Now I don't know what kind of understanding you have of bat. My current feeling is that bat is a combination of DOS commands. You write all DOS commands into the bat command. As long as you run the bat, it will execute the DOS commands one by one. This undoubtedly provides a lot of convenience. Here are some more examples for you. Delete default shares: I don't know how much you know about default shares. Anyway, leaving them is a hidden danger. The only way now seems to be to make a bat file to delete them. The commands are 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 here are your drive letters. If you only have one partition, then write to net share c$ /delete. If you have n partitions, then write them one by one. net share d$ /delete net share e$ /delete net share f$ /delete net share g$ /delete…… Shortcut to log in to a machine on the local area network (the other machine has a password and is a 2000 or later system) net use \\192.168.0.1 /user: explorer \\192.168.0.1 Bat file to back up the registry set regfile=%date% //Set a variable, and the following %regfile% will automatically replace "current day date" if exist "%regfile%" goto end //If a directory named current day date is found, jump to the end of the file. md temp //Create a 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 a bat file to move the directory named "current day date" to temp. md "%regfile%" //Create a directory named current day date cd "%regfile%" //Enter reg export hkcu hkcu.reg //Export the registry reg export hklm hklm.reg //HKEY_CURRENT_USER Abbreviation hkcu. Stores personal data of the current user //HKEY_LOCAL_MACHINE Abbreviation hklm. Core data of the system cd.. deltree /y temp >nul ////Return to the upper directory , delete the temp folder :end |
|
| Floor2 d1998o | Posted 2006-08-04 14:34 |
| 初级用户 Posts 21 Credits 42 | |
|
The batch processing learning materials are well - written and have clear organization, which is relatively useful for beginners like us. Give it a thumbs up.
|
|
| Floor3 jmm988 | Posted 2006-08-08 06:15 |
| 初级用户 Posts 18 Credits 37 | |
|
Thanks to the LZ for imparting knowledge in his busy schedule! I hope to learn about the conditional statements in batch processing under pure DOS, such as "less than or equal to ≤ system time ≥ greater than or equal to", then execute another command. Can you please give me some guidance? Thanks again!
|
|
| Floor4 front | Posted 2007-11-16 20:36 |
| 新手上路 Posts 4 Credits 8 From 福建 | |
|
This sentence is a bit unclear. Just deleting TEMP is enough, why add a nul? What's the purpose?
|
|
| Floor5 cay6200 | Posted 2007-11-16 20:50 |
| 初级用户 Posts 26 Credits 65 | |
|
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 a bat file to move the directory named with the current date to temp. md "%regfile%" //Create a directory named with the current date cd "%regfile%" //Enter reg export hkcu hkcu.reg //Export registry reg export hklm hklm.reg //HKEY_CURRENT_USER is abbreviated as hkcu. Stores personal data of the current user //HKEY_LOCAL_MACHINE is abbreviated as hklm. Core data of the system cd.. deltree /y temp >nul //Return to the upper level directory, delete the temp folder :end Don't understand |
|
| Floor6 hanlongtca | Posted 2007-12-03 20:18 |
| 新手上路 Posts 6 Credits 14 | |
|
Indeed, we beginners really need such materials. To be honest, other things make me confused at a glance, let alone understand them. But I still want to learn, so I'm very confused. It's great to find such an article. I hope experts will write more such articles for us rookies to learn from in the future. Thank you
|
|
| Floor7 mmsknc93 | Posted 2008-02-18 06:44 |
| 新手上路 Posts 6 Credits 14 | |
|
Great, topped.
In the line for /L %%e in (1,1,10) do echo "i am the best" should be for /L %e in (1,1,10) do echo "i am the best" There is only one % sign before e. |
|
| Floor8 davis | Posted 2008-02-23 23:26 |
| 新手上路 Posts 2 Credits 4 | |
|
It's okay, there are a few small mistakes. Just correct them and it'll be okay.
|
|
| Floor9 ksjifjui | Posted 2008-05-30 19:57 |
| 新手上路 Posts 6 Credits 12 | |
|
That's great! Although I don't know much about DOS, I feel that this大哥's explanation is really detailed. I think even the stupidest person should understand it. In fact, batch processing is really practical. I made a simple automatic shutdown batch processing by myself! It's great, and it's more practical than some automatic shutdown software on the Internet. I support this大哥! Support! Support!
|
|
| Floor10 liminghack | Posted 2008-06-01 00:57 |
| 初级用户 Posts 25 Credits 51 From 学校 | |
|
It's quite comprehensive. It would be even better if there were more examples!
|
|
| Floor11 ufwau | Posted 2008-07-22 16:33 |
| 新手上路 Posts 7 Credits 11 | |
|
So dense and numerous, how can a novice have the patience to read
|
|
| Floor12 fengjian | Posted 2008-07-29 16:25 |
| 新手上路 Posts 8 Credits 16 | |
|
Not bad
But I don't understand the last part |
|
| Floor13 shenjianfeng | Posted 2009-02-07 14:58 |
| 新手上路 Posts 3 Credits 4 From 河南省 | |
|
Thanks, but I don't understand it very much.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |