Bat Tutorial: section 4 (variable in batch processing)

Source: Internet
Author: User

Bat tutorial:Section 4(Variable in Batch Processing)

Variable in Batch Processing,I divided him into two types:,Are"SystemVariable"And"Custom variables"

System Variables :
Their values are automatically assigned by the system based on predefined conditions. , That is, these variable systems have defined values for them. ,
We don't need to assign a value to him. ,
% Allusersprofile% Returns the "All users" configuration locally. File .
% Appdata % Local return by default Application Program Storage Data .
% CD % Returns the string of the current directory locally.
% Too many line % Local return is used to start the current cmd . EXE Accuracy Command Line.
% Cmdextversion % The system returns the version number of the current "command handler extension.
% Computername % The name of the computer returned by the system.
% Comspec % The system returns the exact path of the executable program of the command line interpreter.
% Date % The system returns the current date. Use and date / T command in the same format. By cmd. EXE generation. Related
For details about the date command, see date.
% Errorlevel % The system returns the error of the previous command. Code . A non-zero value is usually used to indicate an error.
% Homedrive % The system returns the local workstation drive letter that is connected to the user's home directory. Based on the main directory Value Set . Use
The home directory is specified in "local users and groups.
% Homepath % The system returns the complete path of the user's home directory. Set Based on the main directory value. The main directory of the user is
User and group.
% Homeshare % The system returns Network Path. Set Based on the main directory value. The user's main directory is
Specified in "local users and groups.
% Logonserver % Returns the name of the domain controller that verifies the current logon session.
% Number_of_processors % The number of processors installed on the computer.
% OS % System returnOperating System Name. Windows 2000 the operating system of windows _ NT is displayed.
% Path % The system specifies the search path for executable files.
% Pathext % The system returns a list of file extensions that the operating system deems executable.
% Processor_architecture % The chip architecture of the system return processor. Value: x86 or IA64 based
Itanium
% Processor_identfier % The system returns the processor description.
% Processor_level % The system returns the model of the processor installed on the computer.
% Processor_revision % The system returns the version number of the processor.
% Prompt % Return the command prompt settings of the current interpreter locally. By cmd . EXE generation.
% Random % The system returns any decimal number between 0 and 32767. By cmd . EXE generation.
% Systemdrive % Windows server operating is returned. System Root directory (system root directory)
.
% Systemroot % The system returns Windows server operating. System The location of the root directory.
% Temp % And % TMP % The system and the user return the default temporary directory used by the applications available to the current login user.
Some applications require temp, while other applications require TMP.
% Time % The system returns the current time. Use and Time / T command in the same format. By cmd . EXE generation. Related
Time For more information about commands, see time.
% Userdomain % Returns the name of the domain containing the user account.
% Username % Returns the name of the user currently logged on.
% USERPROFILE % Returns the location of the configuration file of the current user.
% Windir % The location of the operating system directory returned by the system.
Such multi-system variables , How do we know what his value is? ?
Input echo in cmd % Windir %
WINDIR variable name ,

For example , For example, you can copy the file to the startup directory of the current account.
Copy D : \ 1 . Bat "% USERPROFILE % \ Start Menu \ Program \ Start \"

% Username % Returns the name of the user currently logged on. Note that directories with spaces must be enclosed in quotation marks.
There are also some System Variables , They mean one thing. ,
They are % 0 % 1 % 2 % 3 % 4 % 5. . . . . . Until % 9. % *
% 0. This is special. , Several layers , First lecture % 1 - % 9. .
% 1. Return the first parameter of the batch processing.
% 2. Return the second parameter of batch processing.
% 3 - % 9 according to this category
Reverse Batch Processing Parameters ? What is the return method? ?
Let's look at this example. , Save the following code as test . BAT and put it on drive C.
@ Echo off
Echo % 1 % 2% 3 % 4
Echo % 1
Echo % 2
Echo % 3
Echo % 4
Go to cmd , Input CD C : \
Enter Test . Bat I am the first parameter I am the second parameter I am the third parameter I am the fourth Parameter
Pay attention to the blank amount in the middle , We will see this result :
I am the first parameter. I am the second parameter. I am the third parameter. I am the fourth parameter.
I am the first parameter
I am the second parameter
I am the third parameter
I am the fourth Parameter
Compare the code , % 1 indicates that I am the first parameter. % 2. I am the second parameter.

These % 1 and % 9. batch processing can also run with Parameters ,
There is another % * What is he? ? He is not very useful , Only return parameters. , However, all parameters are returned at one time.
Value , Do not enter % 1 % 2. Determine
Example
@ Echo off
Echo % *
Save as test . Put bat on drive C
Go to cmd , Input CD C: \
Enter Test . Bat I am the first parameter I am the second parameter I am the third parameter I am the fourth Parameter
We can see that all the parameters are displayed at one time.
Let's start talking about the special one. % 0
% 0. This is not the value of the returned parameter. ,
Meaning of the first layer : Returns the absolute path of the batch processing.
Example :
@ Echo off
Echo % 0
Pause
Save as test . Run bat on the desktop , The following result is displayed.
"C: \ Documents and Settings \ Administrator \ Desktop \ test. Bat"
He printed the path of the current batch processing execution. , This is the absolute path of the returned batch processing.
Level 2 : Execute bat in an infinite loop
Example :
@ Echo off
Net user
% 0
Save as BAT for execution , It will execute the net user command in an infinite loop., Until you manually stop .
The above are some system variables in the batch processing. , There are also some variables , They also represent some features ,
In the for command , For variable already said , No more .
Custom Variables
Hence the name , Custom variables are the variables that we assign to them.
To use custom variables, you must use the set command. , Look at the example .
@ Echo off
Set VaR = I am a value
Echo % VaR %
Pause
Save as BAT for execution , We will see "I am a value"
VaR is the variable name. , = The value to be given to the variable
This is the simplest way to set variables.
If you want to manually enter the variable value , Instead of specifying , You can use the set command/ P Parameter
Example :
@ Echo off
Set / P VaR = Enter the variable value
Echo % VaR %
Pause
VaR variable name = Prompt on the right , Not a variable value

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.