Use of a batch file (. bat) under Windows

Source: Internet
Author: User
Tags clear screen echo b

Batch processing, which is batch processing, the first three letters of batch file suffix bat are usually considered as a simplified scripting language , which is used in DOS and Windows systems. A batch file is a text file with a. bat or. cmd extension that contains one or more commands that are interpreted by a DOS or Windows system-embedded command interpreter. Although the batch program runs in a command-line environment, it is not only possible to use command-line software, and any programs that are running under the current system can be run in a batch file .

The batch file is a text file that is unformatted. Type the name of the batch file at the command prompt, or double-click the batch file, and the system calls Cmd.exe to run them one by one in the order in which the commands appear in the file . A batch file is a simple program that controls the flow of command runs through conditional statements (if) and Process Control statements (GOTO), and you can use circular statements (for) to loop through a command in a batch. Each well-written batch file is equivalent to a DOS external command, and you can put its directory in your DOS search path (path) so that it can run in any location.

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

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

Non-DOS commands can be used entirely in batch files, and even ordinary data files that do not have executable attributes can be used.

Batch common commands: Batch commands are case insensitive

1. Echo: Displays the character after this command;

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

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

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

5. Call: Calls another batch file (if you call the other batch file directly, after executing that command will not be able to execute the current file subsequent command);

6. Start: Call external programs, all DOS and command-line programs can be called by the Start command, and so on after the external program is completed before continuing to execute the remaining instructions;

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

8. REM: With: Similar, the word after this command identifier explain the line, do not execute, just give yourself to look for in the future;

9.%: Represents a parameter, which is a string added to the file name when the batch file is run, the variable can be represented from%0 to%9,%0 for the file name itself, and the string is represented by the%1 to%9 sequence;

If: The decision is made to comply with the prescribed conditions, thereby determining the execution of different orders;

A. For: Loop command, which executes the same command multiple times as long as the condition is met;

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

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 process;

Set: Show, set, or delete variables, the variables in the batch are not differentiated, and do not need to distinguish between int, float, char, and so on, as in the C language.

. CLS: Clear screen;

Ver: Show version

>, >>: Both the output redirect command, which is to write the output of the preceding command to a file, the difference is that ">" will erase the contents of the original file and write the new content to the original file, and ">>" will only append new content to the original file on another line , without altering the original content;

18.ERRORLEVEL: Each command runs the end, you can use this command line format to view the return code, the default value is 0, General Command execution error will be set ERRORLEVEL 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 ...

Reference documents:

1. Aymx7ckgvnqkgaoaptiaa8oxivflb8ygosyvnrm0dke5i5mr0ahjgvltwyzaic6l3qylk-yxggwyghwcyfqbxq

2. Http:// P5oz6xazb2sw2tb14hcpuih9sml2g76vieigo64vsmyhu-d3asey_lsskygapu3e3jm

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of a batch file (. bat) under Windows

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.