Use of a batch file (. bat) under Windows

Source: Internet
Author: User
Tags clear screen echo b

batch, which is batch processing. Batch file suffix bat takes the first three letters and is often considered a simplified scripting language for use in DOS and Windows systems. A batch file is a text file with a. bat or. cmd extension that includes one or more commands that are interpreted by a DOS or Windows system-embedded command interpreter. Although the batch program executes in a command-line environment, it is not only possible to use command-line software, but any program that can be executed under the current system can be executed in a batch file .

The batch file is a text file that is unformatted. at the command prompt, type the name of the batch file, or double-click the batch file. The system calls cmd.exe to execute them one by one according to the order in which each command appears in the file . A batch file is a simple program that can control the flow of command execution through conditional statements (if) and Process Control statements (GOTO). You can also use a loop statement (for) to loop through a command in a batch process.

Each well-written batch file is equivalent to a DOS external command, and you can put its folder in your DOS search path (path) so that it can be executed in any location.

The nature of batching is the set of DOS commands that are arranged in a certain order. Whatever commands you can use at the command prompt are available in the batch program .

Format of batch file: Each line can be treated as a command, each command can contain a multi-note command, starting from the first line execution, until the end of the last line, it executes the platform is DOS.

The batch file is fully capable of using non-DOS commands and can even use generic data files that do not have executable characteristics.

Batching often uses commands: Batch commands are not distinguished by uppercase and lowercase

1. Echo: Displays the characters after this command.

2. Echo. : Displays a blank line (equivalent to the result of entering a carriage return).

3. Echo off: All commands executed after this statement do not display the command line itself.

4. @:@ is similar to echo off, but it is added to the front of the other command line, indicating that the command line itself is not displayed at execution time;

5. Call: There is also a batch file called (assuming that a direct call to another batch file, after executing that command will not be able to execute the current file May command).

6. Start: Call an external program. All DOS commands and command-line programs can be called by the start command. After the completion of the external procedures, the remaining instructions continue to execute;

7. Pause: Execute this sentence will pause, display Pressany key to continue ... Wait for the user to press the random key to continue;

8. REM: Similar to:, the word identifier after this command interprets the line. Do not carry out, just for their future search use;

9.%: Indicates the number of references. A parameter is a string that is added to the file name when the batch file is executed. Variables can be from%0 to%9. %0 represents the file name itself, and the string is represented in the%1 to%9 order.

If: The inference is to conform to the prescribed conditions, thus determining the execution of different commands.

A. For: Loop command, which executes the same command more than once, only if the condition is met.

Choice: In a batch program, the user is able to select from a set of option keys by displaying the prompt and pausing the batch process;

Goto: Jump command, the program pointer jumps to the specified label, starting from the first command after the label to continue the execution of the batch program.

Set: Displays, sets, or deletes variables that are not type-sensitive in batches. There is no need to distinguish between int, float, char, and so on, as in C language variables;

The. CLS: Clear screen.

Ver: Show version number

>, >>: Both the output redirect command, which writes the output of the preceding command to a file, the difference is that ">" erases the contents of the original file and writes the new content to the original file. and ">>" will only add new content to the original file, but will not modify the original content;

18.ERRORLEVEL: The end of each command execution. Can use this command line format to view the return code, the default value is 0, the General Command execution error will be set ERRORLEVEL to 1;

Here's a simple example: Autotest.bat

@echo off:: Auto run unit Testmkdir "E:/TMP/32" mkdir "E:/TMP/64" Echo%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time : ~3,2%%time:~6,2%echo.echo start Auto test ... set exepath=e:/tmp/api_x64/binif "%1" = = "Set exepath=e:/tmp/api_x86/" binif "%1" = = "All" set Exepath=e:/tmp/api_x64/bin:: Echo%1set srcimagepath=e:/tmp/testimagesset dstimagepath=e:/tmp/%1 :: Echo%dstimagepath%set txtoutput=%dstimagepath%/result.txt:: Echo%txtoutput%echo A.exe >>%txtoutput%start/b /wait%exepath%/a.exe%srcimagepath%/cat.jpg%dstimagepath%/result_cat.jpg >>%txtoutput%echo A.exe%errorlevel %echo.echo B.exe >>%txtoutput%start/b/wait%exepath%/b.exe%srcimagepath%/dog.jpg%dstimagepath%/result_ Dog.jpg >>%txtoutput%echo B.exe%errorlevel%echo.echo end Auto Test ...

References:

1. http://baike.baidu.com/link?url= Aymx7ckgvnqkgaoaptiaa8oxivflb8ygosyvnrm0dke5i5mr0ahjgvltwyzaic6l3qylk-yxggwyghwcyfqbxq

2. Http://wenku.baidu.com/link?url=SbWxLyV0sjiDgk0Cv5A0GzpR5x-d10xF1qfSbVi_ P5oz6xazb2sw2tb14hcpuih9sml2g76vieigo64vsmyhu-d3asey_lsskygapu3e3jm

Use of a batch file (. bat) under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.