Description: The extension is bat file is a batch file ________________________________________________________________________________________________ First, the batch file is a text file that each line is a DOS command (most of the time as if we perform at the DOS prompt The command line is the same), you can create and modify the batch file using any text file editing tools such as Edit or Windows Notepad (NOTEPAD). Second, the batch file is a simple program that can control the process running through the Conditional Statement (IF) and Process Control Statement (GOTO), and can also use the loop statement (for) to perform a command in the batch. . Of course, the programming capabilities of batch files are very limited, and it is very unregulated. Batch program statement is a DOS command (including internal commands and external commands), and the capacity of batch depends primarily on the command you are using. Third, each prepared batch file is equivalent to a DOS external command, you can put it in your DOS search path (PATH) to make it run in any location. A good habit is to create a BAT or BATCH directory (for example, C: / Batch) on the hard disk, then put all your written batch files in this directory, so you just set up C: / Batch in PATH, you You can run all your written batch programs in any location. Fourth, under the DOS and WIN9X / ME system, the autoexec.bat batch file under the root directory is automatically run batch file. You can run the file each time the system is started, you can start the system each time The command to run is placed in this file, such as setting the search path, tune the mouse driver and disk cache, set system environment variables. Below is an example of AutoExec.bat running under Windows 98: @echo offpath C: / Windows; C: / Windows / Command; C: / Ucdos; C: / Dostools; C: / SyStools; C: / Wintools; C: / baith smartdrv.exe / xlh doskey.com / INSERTLH CTMouse.exeset Temp = D: / TEMPSET TMP = D: / TEMP batch effect Simple, the role of batch is automatic continuous execution multiple commands. Here you will talk about a simplest application: When you start the WPS software, you must execute each time (> previous content represents DOS prompt): C: /> CD WPS C: / WPS> SPDoSC: / WPS> PY C: / WPS> WBX C: / WPS> WPS If you do this before every time you use WPS, do you feel very trouble? Ok, use the batch, you can simplify these troubles, first we write a runwps.bat batch file, the content is as follows: @echo offc: CD / WPSSPDoSPYWBXWPSCD / after we enter WPS, only need Run the runwps this batch file. Common ordered commands echo, @, call, pause, remote is a few commands that are most commonly used by batch files, and we start learning from them.
Echo indicates that the character echo off after this command indicates that all run commands after this statement does not display the command line itself @ and the echo offline, but it is the front of each command line, indicating that the runtime does not display The command line of this line (only the current row can be affected). Call calls another batch file (if you do not use Call to directly call other batch files, then the current file will not be returned after the batch file is executed and the current file will be executed. PAUSE runs this sentence to suspend the execution of the approve and display the prompts of the press any key to contact ... on the screen, wait for the user to continue REM to continue REM to indicate the character after this command is explained (notes), not executed, just Give yourself in the future reference (equivalent to the comments in the program). Example 1: Edit a.bat file with Edit, enter the following contents After the store is c: /a.bat, you can implement it after executing the batch file: write all the files in the root directory to A.TXT, start UCDOS, enter WPS and other functions. The content of the batch file is: Command Note: @echo off does not display the subsequent command line and the current command line DIR C: /*.*> A.txt Write the C-disk file list to a.txt call C: / UCDOS / UCDOS .bat call ucdos echo Hello, Hello, "Hello" Pause Pause, wait button to continue REM Preparing to run WPS Note: Preparing to run WPS CD UCDOS to enter the UCDOS directory WPS to run the parameter batch file for WPS batch files can also be like a function of C language The same use parameters (equivalent to the command line parameters of the DOS command), which requires a parameter representator "%". % [1-9] indicates parameters, and parameters refer to a string separated by space (or Tab) after running the batch file. Variables can indicate the batch command itself from% 0 to% 9,% 0, and other parameter strings are represented by% 1 to% 9. Example 2: C: The root directory has a batch of processing file names f.bat, the content is: @echo offformat% 1 If you perform C: /> FA: So when F.BAT is executed,% 1 represents A:, Format% 1 is equivalent to Format A: The above command actually executes Format A: Example 3: C: The root directory The next batch of processes file name is T.BAT, the content is: @echo offty% 1 TYPE % 2 runs C: /> T a.txt b.txt% 1: Represents A.TXT% 2: Indicates that B.TXT The above command will sequentially display the contents of the A.TXT and B.TXT files. Special Command If Goto Choice for is a relatively advanced command in a batch file. If you use it very well, you are an expert who batch files. First, if the IF is a conditional statement, it is used to determine whether it meets the specified conditions, thereby deciding to perform different commands. There are three formats: 1, if [not] "parameter" == "string" The command parameter to be executed is equal to (NOT means not etiquette, the same) The specified string, the condition is established, run the command, otherwise running One sentence.
Example: if "% 1" == "a" Format A: 2, if [NOT] Exist [Path /] file name to be executed If there is a specified file, the condition is established, run the command, otherwise run the next sentence. Such as: if exist c: /config.sys type c: /config.sys Indicates that it is displayed if there is a C: /Config.sys file. 3, if Errorlevel
If the tool detects an error status, it returns 255 ErrorLevel value. If the user presses the Ctrl Break or Ctrl C button, the tool will return to the ErrorLevel value. Use the Errorlevel parameter in a batch to place the parameters. Example: choice /? Choice / c ync / m "Confirm, press Y, please press N, or cancel Please press C." Choice / T 10 / C YNC / CS / D Y Choice / C AB / M "option 1 Please select a, option 2, select b. "Choice / C Ab / N / m" option 1 Please select a, option 2 Please select B. "If I run the command: choice / c ync / m" confirm, please press Y, please No Please press N or cancel it. "The screen will be displayed: Confirm, press Y, no, press N, or cancel, press C. [Y, N, C]? Example: Test.bat is as follows (note, when the return value is used to determine the return value with if ErrorLevel, press the return value from high to low alignment): @echo off choice / c dme / m "defrag, MEM, END "if Errorlevel 2 Goto Mem if Errotlevel 1 Goto Defrag: Defrag C: / DOS / DEFRAG GOTO end: MEM MEM GOTO end: End echo good Bye After this batch is running," Defrag, MEM , END [D, M, E]? ", the user can select DME, then the IF statement determines according to the user's selection, D represents the block segment that executes the label DEFRAG, and m represents the block of the label MEM, e means execution The block of the label is END, each block is finally jumped at the END Number with goto End, and then the program will display the good bye and the batch run ends. Fourth, for loop command, as long as the condition is in line with, it will execute the same command multiple times. Syntax: Perform a particular command to each file in a set of files. For %% Variable In (SET) Do Command [Command-Parameters] %% Variable Specifies a single letter replaceable parameter. (SET) Specify one or a set of files. You can use wildcards. Command specifies the command to execute each file. Command-parameters specifies parameters or command line switches for a specific command. For example, there is a line in a batch file: for %% c in (* .bat * .txt) Do Type %% C then display all the contents of the current directory to the files with BAT and TXTs as extensions. Batch example 1. If-exist1) First use Notepad at C: / create a TEST1.BAT batch file, the contents are as follows: @echo off f eExec.bat if NOT TYPE /ATOEXEC. Bat echo /autoexec.bat does not exist then runs it: c: /> Test1.bat If the c: / exists, the content will be displayed, if there is no existence, the batch will prompt you if it does not exist. This file does not exist.
2) Then then build a Test2.bat file, the content is as follows: @echo off if exist /% 1 TYPE /% 1 if not exist /% 1 ECHO /% 1 Does NOT EXIST execution: c: /> Test2 autoec.bat The result of the command is equal. Description: (1) if exist is used to test whether the file is existing, the format is the% 1 of the IF exist [Path File Name] command (2) Test2.bat file is the parameter, the DOS allows 9 batches of parameter information Batch file,% 1 ~% 9 (% 0 indicate the Test2 command itself), this is a bit of the relationship between the arguments and metall parameters in the programming, and% 1 is the ginseng, autoexec.bat is the argument. 3) Further, establish a file called Test3.bat, the content is as follows: @echo offif "% 1" == "a" echo xiao if "% 2" == "b" echo tian if "% 3" == "C" echo xin If running: c: /> Test3 ABC screen will display: xiaotianxin If you run: C: /> Test3 AB screen Display xiaotian During this command execution, DOS will take an empty string Specifies to parameter% 3. 2, if-errorlevel establishes TEST4.BAT, the content is as follows: @echo off xcopy c: /autoexec.bat D: if Errorlevel 1 Echo file copy failed IF Errorlevel 0 Echo success copy file then execute file: c: /> Test4 If file The copy is successful, the screen will display "Successful Copy File", otherwise "file copy failed" will be displayed. If ErrorLevel is used to test the return value of its previous DOS command, pay attention to the return value of the last command, and the return value must be determined in order from large to small order. So the following batch file is wrong: @echo off xcopy c: /autoexec.bat D: / if Errorlevel 0 Echo success copy file if Errorlevel 1 Echo Did not find copy file If ErrorLevel 2 Echo user via CTRL-C suspension copy operation IF Errorlevel 3 Echo Preset Error Block File Copy Operation If Errorlevel 4 Echo Copy Process During the copy, the copy is successful, followed: Did not find a copy file user via Ctrl-C Abort copy operation preset error Block file copy operation copy process The written disk error will be displayed.