Teach you to make batch files

xiaoxiao2021-03-06  13

Teach you to make batch documents (Wenshang Changan) 2004-8-18 13:23:57

The batch file is sometimes useful, in our forum, the most commonly used it may be the batch file of this repair IE (regardless of the IE problem, as long as the file is running, there will be effect): http://www.cfanclub.net/new_dnzs / 040115001a.htm Second, solve 2000, XP default sharing has also a way to make a batch file to "start".

The extension is a file that BAT (or CMD in NT / 2000 / XP / 2003) is a batch file.

First batch file is a text file, each line of this file is a DOS command (most of us seem to be the same as the command line executed by the DOS prompt), you can use the Edit or Windows of Windows under DOS. Any text file editing tool such as this (NOTEPAD) creates and modifies the batch file.

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: / Batchlh smartdrv.exe / xlh doskey.com / insertlh ctmouse.exeset temp = d: / tempset tmp = d: / temp

The role of batch is simple, and the role of batch is automatically continuously implemented 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 a batch, you can simplify these troubles, first we write a runwps.bat batch file, as follows: @echo offc: CD / WPSSPDOSPYWBXWPSCD /

In the future, we have access to WPS each time, just run the running of RunWPs.

Common command

Echo, @, call, pause, remote: Use :: Replace REM) is the most commonly used commands of the batch file, 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, you show "You Good "pause pause, wait button to continue REM ready to run WPS Note: Prepare to run WPS CD UCDOS to enter UCDOS directory WPS run WPS

Batch file parameters

Batch files can also use parameters like the C language function (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: There is a batch of processing files named f.bat in the root directory: @echo offformat% 1

If C: /> f A: So when F.BAT is executed, the% 1 represents A:, so FORMAT% 1 is equivalent to Format A:, then the above command is actually executed, format A:

Example 3: C: The next batch of processing files in the root directory is T.BAT, the content is: @echo offty% 1 TYPE% 2

Then run 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 are very skilled, you are an expert in 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" command to be executed

If the parameter is equal to (NOT means not et al, the following) The specified string, the condition is established, run the command, otherwise run the next 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 The command to be executed

Many DOS programs return a numeric value after running, it is used to indicate the result (or status) of the program run (or status), and the return value of the program can be determined by the if ErrorLevel command, determine the execution of different commands according to different return values ​​(return value must Arranged from large to small order). If the return value is equal to the specified number, the condition is established, run the command, otherwise run the next sentence.

If Errorlevel 2 Goto X2

Second, the GOTO batch file is running here will jump to the reference numeral specified by Goto (label, label is defined by: Command group.

Such as:

Goto end

: End echo this is the end

The label is defined with the ": string", and the routing is not executed.

Third, Choice uses this command to allow users to enter a character (for choice) to return different ErrorLevel according to the user's selection, and then run different commands according to the user's selection.

Note: The choice command is the external command provided by the DOS or Windows system. Different versions of the Choice command speech will be slightly different. Please use the khoice /? View.

Choice's command syntax (this syntax is the syntax of the choice command in Windows 2003, and the other version of the Choice command syntax is similar to this.):

Choice [/ cchoices] [/ n] [/ cs] [/ t timeout / d choice] [/ m text]

Description: This tool allows users to select an item from the selection list and return the index of the selected item.

Parameter list: / c Choices Specifies the list of options to create. The default list is "YN".

/ N hides the list of options in the prompt. Tip The previous message is displayed, and the option is still enabled.

/ Cs allows selection options to select. By default, this tool is in case case.

/ T timeout makes the number of seconds before the default selection. Acceptable values ​​are from 0 to 9999. If 0 is specified, there will be no pause, the default option will be selected.

/ D Choice Specifies the default option after NNNN seconds. The characters must be in a set of selections specified by the / c option; at the same time, NNNN must be specified with / t.

/ M text Specifies the message to display before prompt. If not specified, the tool only shows the prompt.

/? Display help messages.

Note: ErrorLevel environment variable is set to the key index selected from the selection set. The first selection list will return 1, and the second selection returns 2, and so on. If the user presses the key is not a valid choice, the tool will warn the sound. 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 3 Goto Endif 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 run, "DEFRAG, ME, END [D, E]?", The user can select D M E, then the IF statement determines based on the user's selection, D represents the block segment executing the label DEFRAG. , M represents a block that executes the label MEM, e means executing the block END, and each block is finally jumped to the END Number with goto end, and then the program will display the goodbook, the batch run.

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

The command line displays all the contents of the files with BAT and TXT-based files in the current directory.

Batch example

IF-EXIST

1)

First use the notepad in C: / create a TEST1.BAT batch file, the file content is as follows: @echo off @ fTOEXEC.BAT if not exist /autoexec.bat echo /autoexec.bat does not exist

Then run it: c: /> test1.bat If the autoeexec.bat file, then its content will be displayed, if there is no existence, the batch will prompt you if the 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

Execute: c: /> Test2 autoexec.bat This command runs the result.

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 you run: c: /> Test3 a b c screen is displayed: xiaotianxin

If you run: c: /> Test3 a b screen will display xiaotian

During this command execution, DOS specifies an empty string to the parameter% 3.

2, if-errorlevel

Establish 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 the file: c: /> TEST4

If the file 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 Write Error

Whether the copy is successful, followed by:

Did not find a copy file user via the Ctrl-C suspension copy operation preset error Block file copy operation copy process during writing

Will be displayed.

The following is the return value of several common commands and its representatives: Backup 0 Backup Success 1 Did not find a backup file 2 file sharing conflict block backup Complete 3 Users Ctrl-C Stop Backup 4 Due to fatal errors to abort the backup operation

DiskComp 0 disk compares different 1 disk comparison 2 users suspended comparison operations via Ctrl-C 3 Due to fatal errors 4 preset error abort

DiskCopy 0 disk copy operation success 1 Nelfally read / write error 2 users End copy operation 3 due to fatal processing errors to copy the disk 4 preset error Block copy operation

Format 0 Format Success 3 Users Survey Format Treatment by Ctrl-C Stopping Formatting Aborted 5 In prompt "Proceed with format (y / n)?" User Type N end XCOPY 0 Success copy file 1 Did not find a copy file 2 user via Ctrl-C suspension copy operation 4 Preset error Block file copy operation 5 copy procedure during writing

3, if string1 == String2

Established TEST5.BAT, the file content is as follows: @echo off if "% 1" == "a" format A:

Perform: C: /> Test5 A screen appears to form a: disk formatted content.

Note: In order to prevent the parameters from being empty, the string will generally use double quotes (or other symbols, notice that the reserved symbol) is enclosed. Such as: IF [% 1] == [a] or if% 1 * == a *

5, goto

Establishing Test6.bat, the contents of the document are as follows: @echo offixec.bat goto _copy goto _done: _Copy Copy C: / /autoexec.bat d: /: _done

Note: (1) Before the label is the ASCII character's colon ":", there is no space between the colon and the label. (2) Name rules of the label are the same as the name rules of the file name. (3) DOS supports the label of the longest eight characters. When it is not possible to distinguish two labels, it will jump to the nearest label.

6, for

Established C: / Test7.bat, the file content is as follows: @echo off for %% C in (* .bat * .txt * .sys) do type %% C

Run: C:> TEST7

After execution, all the C: Panhe TXT, SYS is displayed in BAT, TXT, and SYS (excluding the hidden file).

转载请注明原文地址:https://www.9cbs.com/read-49417.html

New Post(0)