Windows Batch File

Source: Internet
Author: User

The window closes automatically: After the batch file executes, the window closes automatically, and if the window does not close automatically when it is finished, add
1. After the batch file finishes executing, the window will automatically close
2. If the window does not automatically close after execution, add pause at the end of the file.

REM is a comment command is usually used to annotate the program, and the content of the command will not be displayed and executed when the program executes.

@ In a batch means to turn off the echo of the current line. We know from above that the command echo off can turn off the entire batch of commands echoing but not the echo off command. Now we add @ To this command and echo off this command is turned off by @ To achieve all the commands do not appear to require

Batch input line breaks individually
echo. and echo/enter separate line breaks, there is no space between echo and Dot, Slash, and the Echo is displayed in the Echo current state, and if echo off is previously enabled, the hint is "echo is off" and if "echo on" is used earlier, The hint is "echo is turned on."

Set: Set or modify variable values, syntax set [variable=[string]]
Variable is the variable name, string is the value specified for the variable (a system string)

Note: The variable name is immediately followed by an equal sign (=), there is no space in the middle, otherwise the assignment fails. There can be spaces after the equals sign (=), but spaces appear as characters. Therefore, when using set assignment, there should be no spaces before and after the equals sign!

For example:
Set var=2
Echo%var%

When set specifies a variable value, if the variable already exists in the environment, the old value is replaced with the new value, and only the old value is cleared if there is no value after the equal sign.

"Variable View"
View all current system variables, enter set directly, followed by no parameters.
Look at all the series of environment variables (that are variables) that begin with a letter, as long as the set command is preceded by a first letter, such as displaying all variables starting with P, enter: set P. If a variable name starting with P is not found in the current environment, the ERRORLEVEL is set to 1.

"Variable Reference"
When referencing variables, each side of the variable name needs to be expanded with a percent sign (%), such as%var%.

"Variable Assignment"
In addition to the basic variable assignment, set has two ways of assigning values with parameters,/A and/P, which greatly enhance the function of set, as follows:

/P
Interactively assigns the value of the variable to the value entered by the user, before reading the user input value, can have prompt string (also can not write hint string, but usually have), for example:


@echo off
set/p name= Please enter your user name:
set/p pw= Please enter your password:
If%name% = = Name (echo you enter username: name) Else (echo user name input error)
if%pw% = = 123456 (echo you enter the password is: 123456) Else (echo password input error)
Pause

The above code is just a simple test, in practical use is very important, for example, we can let the user enter a user name, password, and then go to connect the Oracle database, to execute the script in the specified folder.

"Common system Variables"
%date%: current date.
%time%: Current time.
Any decimal number between%random%:0 and 32767.
%errorlevel%: Current ERRORLEVEL value.
%0: Current full full path (with double quotes)
%~0: Current full full path (matchless quotes)
%~S0: Current short full path name (matchless quotes)
%~F0: Current Full path
%cd%: Current directory (no file name)
%~D0: Current drive letter
%~P0: Current directory (no drive letter, no file name)
%~n0: Current file name
%~x0: Current file name extension
%~T0: Last modified time for the current file
%~z0: Current file size (B)

Windows Batch File

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.