BAT file Detailed

Source: Internet
Author: User
Tags echo command error code goto parent directory

Batch processing has been one of the most important features since the advent of the operating system, but in the case of Windows, the concept of batch processing has become more and more remote for ordinary users, but admittedly, as a developer, this is one of the skills that must be mastered. Under the Linux system, batch processing is done through shell scripting, while under Windows platform, the batch file suffix is. bat. The bat file is actually a collection of DOS commands, but many DOS commands are deprecated, so let's explain the basic wording of the commands and bat files used in the bat file today.
Basic Commands Echo

This is the most basic display command, which will be displayed in the command line, followed by strings, variables, and so on, basically the same as the echo command in Linux:

echo "Hello,my first Bat."

In practical applications we will combine this command with a redirect symbol (also known as a pipe symbol, usually > >> ^) to implement input commands into a file of a particular format. In the bat file header, the echo off statement is typically used to indicate that the following command line is no longer displayed. This statement is used in conjunction with @, and @ is used before the command to indicate that the current command line is not displayed, but only applies to the current row. So, the first line of the bat file that we see generally is as follows:

@echo off
Pager

As the name implies, this command is used to invoke an external bat file, using the bit call+ "file name". If you call another batch file without calling, you will not be able to return the current file and execute subsequent commands for the current file after executing that batch file.

Call A.txt
REM

Note, some similar to//in the C language, the following content will not be executed, but only for reference when reading the source code. CD

ChDir, believe that people who have used the command line will not be unfamiliar, for switching paths. Some special symbols need to be mastered, the following are basic usage:

REM assumes that the current directory is the root directory of the C disk, which has my, your two folders, the F disk has
his folder rem enters my
rem folder CD I/the
root directory, from my into your
cd/your
rem Return to last catalog
CD-
REM return to parent directory
CD
. REM Returns the root directory
CD. rem or CD/
REM switch to his in the F drive, remember \d cannot omit
CD \d d:/his
Pause

As the name implies, this directory suspends execution of the program, and running this sentence pauses the execution of the batch and displays the press any key to continue on the screen ... The prompt waits for the user to press any key to continue. Start

Calling an external program, all DOS commands and command-line programs can be called by the start command.
Common parameters:
Min Start window minimized
Separate start 16-bit Windows programs in separate spaces
High starts the application in the high priority category
REALTIME starting the application in the REALTIME priority category
Wait to start the application and wait for it to end
Parameters these parameters for transmission to the command/program

Executes an application that is a 32-bit GUI application when CMD. EXE returns a command prompt without waiting for the application to terminate. If executed within a command script, the new behavior does not occur. Choice

Choice Use this command to allow the user to enter a character (for selection), to return different ERRORLEVEL based on the user's choice, and then to run different commands according to the user's choice, if errorlevel mates.
Note: The choice command is a DOS or Windows system-provided external command, different versions of the choice command syntax will be slightly different, please use the choice/? View usage.
The choice parameter list is as follows:
Parameter list:
/C choices Specifies the list of options to be created. The default list is "YN".
/n Hides the list of options in the prompt. Prompts the previous message to be displayed, and the option is still enabled.
/cs allows selection of case-sensitive options. By default, this tool is case-insensitive.
/T timeout before making the default selection, the number of seconds to pause can accept values from 0 to 9999. If you specify 0, there will be no pauses, and the default option will be selected.
/d Choice Specify the default options after nnnn seconds. The character must be in a set of choices specified with the/C option, and nnnn must be specified with/T.
/M text Specifies the message to display before prompting. If not specified, the tool displays only the hint.
/? Displays the help message.

CHOICE/C ync/m "Confirm please press Y, no 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 please select B. "
choice/c ab/n/M" option 1 Please select a, option 2 please select B.
Goto

Specifies to jump to the label, and when the label is found, the program processes the command starting from the next line. The goto command is notorious in many high-level languages because of some of its features, but it is still necessary in scripting languages.

if {%1}=={} goto noparms if 
{%2}=={} goto noparms (if,% 1,%2 if you do not understand, skip over and there will be a detailed explanation later.) ) 
: Noparms 
echo Usage:monitor.bat serverip portnumber 
Goto End
if

The IF indicates that the specified conditions will be judged, thus deciding to execute different commands. There are three types of formats:
1. If "parameter" = = "string" command to be executed
If the argument equals the specified string, the condition is set, run the command, or run the next sentence. (note is two equals)

If "%1" = = "A" format a: 
if {%1}=={} goto noparms 
If exist filename to execute command
If there is a specified file, then the condition is established, run the command, otherwise run the next sentence.
If errorlevel/if not errorlevel number of commands to execute
If the return code equals the specified number, the condition is set up, run the command, or run the next sentence.
if errorlevel 2 goto x2

The program will return a number, called the error code ERRORLEVEL or return code, the common return code is 0, 1. for

The for command is a more complex command that is used primarily for parameters that loop through commands within a specified range. When using the for command in a batch file, specify the variable using%%variable. In fact, and in the high-level programming language for a similar, its syntax is as follows:
For%variable in (set) do command [Command-parameters]
%variable specifies a single-letter replaceable parameter.
(set) specifies one or a set of files. Wildcard characters can be used.
command specifies the commands to execute on each file.
Command-parameters specify parameters or command-line switches for specific commands.

Well, basically the common command is so many, DOS command actually many, such as set, |, shift and so on, in short, to write a good bat file, command familiarity is essential. using variables in Bat

Variables are essential in any language, and of course in the bat file. After you've finished the basic commands, let's talk about how to use variables in the bat file.
%[1-9] Represents a parameter, which is a string separated by a space (or tab) after the file name when the batch file is run. Variables can represent the batch command itself from%0 to%9,%0, and the other parameter strings are represented in the%1 to%9 order.
If there is now a batch of processing files T.bat in the C packing directory, the contents are as follows:

@echo off 
type%1 

Then at the command line input c:/>t a.txt b.txt Run,%1 means that a.txt,%2 means b.txt, and other parameters are deferred. An example illustrates

Of course, to learn the number of any language, the light depends on the grammar is useless, you must analyze the source program, preferably those using a wide range of source programs. In order to facilitate the explanation, I removed the batch file from the Tomcat7 Startup.bat and wrote some instructions in the way of comments, so that we could learn it easily.

@echo off REM Create environment variable for current directory set "current_dir=%cd%" REM determines if the environment variable of the Tomcat installation directory is correct, and if correct, go to gothome if not "%catalina_home%" = = "" Goto gothome REM If not correct, set the current directory to the software root set "catalina_home=%current_dir%" REM Determine if there are catalina.bat batch files present, go to Okhome, otherwise return to parent directory if exist "%catalina_home%\bin\catalina.bat" goto okhome REM
Return to the parent directory and set the software root directory to this directory CD. Set "catalina_home=%cd%" REM Open current directory CD "%current_dir%" rem gothome module: gothome if exist "%catalina_home%\bin\catalina.bat Goto okhome echo the catalina_home environment variable is not defined correctly echo this environment variable is Neede D to run the program goto end REM gothome module end rem okhome module: okhome Set "Executable=%catalina_home%\bin\catalina.bat" R EM Check that target executable exists if exist "%executable%" goto okexec Echo Cannot find '%executable% ' echo this file is needed to run the program goto end REM okhome module end: okexec rem Get remaining unshifted command line arguments and SAV e them in the set cmd_line_args=: Setargs REM Determines if the command line arguments exist if ""%1" "= =" "" "Goto Donesetargs set cmd_line_args=%cmd_line_args%%1 shift goto Setargs:d Onesetargs call"%executable% "STA
 RT%cmd_line_args%: End

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.