Summary of variables in DOS batch files _dos/bat

Source: Internet
Author: User
Tags current time error code

The variables in the batch, I divide him into two categories, "System variables" and "Custom variables"

System variables:

Their values are automatically assigned by the system according to predefined conditions, which means that the variable systems have defined values for them,
Don't need us to assign him a value, we just need to call it! I'll list them all!

%allusersprofile% returns the location of the "All users" configuration file locally.
%appdata% locally returns where the application stores data by default.
%cd% returns the current directory string locally.
%cmdcmdline% returns the exact command line used to start the current Cmd.exe.
The%cmdextversion% system returns the version number of the current command handler extension.
The%computername% system returns the name of the computer.
The%comspec% system returns the exact path of the command line interpreter executable program.
The%date% system returns the current date. Use the same format as the date/t command. Generated by Cmd.exe. About

For more information about the date command, see Date.
The%errorlevel% system returns an error code for the previous command. Errors are usually represented by a value other than 0.
The%homedrive% system returns the local workstation drive letter connected to the user's home directory. Set based on the home directory value. Use

The head of household directory is specified in Local Users and groups.
The%homepath% system returns the full path to the user's home directory. Set based on the home directory value. The user's home directory is in the

To the user and group in the.
The%homeshare% system returns the network path of the user's shared home directory. Set based on the home directory value. The User home directory is

specified in Local Users and groups.
%logonserver% Local returns the name of the domain controller that verifies the current logon session.
The%number_of_processors% system specifies the number of processors installed on the computer.
The%os% system returns the operating system name. Windows 2000 displays its operating system as WINDOWS_NT.
The%PATH% system specifies the search path for the executable file.
The%pathext% system returns a list of file name extensions that the operating system considers executable.
The%processor_architecture% system returns the processor's chipset architecture. Value: x86 or IA64 based on

Itanium
The%processor_identfier% system returns the processor description.
The%processor_level% system returns the model number of the processor installed on the computer.
The%processor_revision% system returns the version number of the processor.
%prompt% returns the command prompt settings for the current interpreter locally. Generated by Cmd.exe.
The%random% system returns any decimal digits between 0 and 32767. Generated by Cmd.exe.
The%systemdrive% system returns the drive containing the Windows server operating system root (that is, the systems root).
The%SYSTEMROOT% system returns to the location of the Windows server operating system root directory.
%TEMP% and%TMP% systems and users return the default temp directory used by applications that are available to the currently logged-on user.

Some applications require TEMP, while other applications require TMP.
The%time% system returns the current time. Use the same format as the time/t command. Generated by Cmd.exe. About

Time command For more information, see time.
%userdomain% locally returns the name of the domain that contains the user account.
%USERNAME% returns the name of the user who is currently logged on locally.
%USERPROFILE% returns the location of the current user's profile locally.
The%WINDIR% system returns the location of the operating system directory.


So many system variables, how do we know what his value is?
Enter Echo%WINDIR% in cmd
windir variable name, not random lose!
This will show the value of a variable!

For example, let's copy the file to the current account's startup directory.

Copy D:/1.bat "%userprofile%/" Start "menu/Program/start/"

%USERNAME% returns the name of the user who is currently logged on locally. Note that directories with spaces are enclosed in quotation marks.


There are also some system variables, they represent a meaning, or an operation!

They are%0%1%2%3%5 ..... Until%9 had a%*.

%0 This is a little special, there are several layers of meaning, first speak%1-%9 meaning.

%1 returns the first parameter of a batch
%2 returns the second parameter of the batch
%3-%9 According to this kind of push

Back batch parameters? What exactly is the return method?

Let's look at this example and save the following code as test. BAT and put it under the C disk.

@echo off
Echo%1%2%3%4
Echo%1
Echo%2
echo%3
Echo%4

Enter cmd, input CD c:/
Then enter Test.bat I'm the first argument I'm the second argument I'm the third argument I'm the fourth argument

Attention to the middle of the vacancy, we will see this result:

I'm the first argument I'm the second argument I'm the third argument I'm the fourth argument
I'm the first parameter.
I'm the second parameter.
I'm the third parameter.
I'm the fourth parameter.

In contrast to the code,%1 is I am the first parameter%2 is the second argument
How to understand it!

These% 1 and%9 can allow batch processing to run with parameters, greatly improving batch processing capabilities!

There's another%*. He's not very big, he's just returning arguments, but he's returning all the arguments at once.

Value without having to determine the input%1%


Example
@echo off
Echo%*

Also save for Test.bat put to C disk
Enter cmd, input CD c:/
Then enter Test.bat I'm the first argument I'm the second argument I'm the third argument I'm the fourth argument

You can see him showing all the parameters at once.


@echo off

For%%i in (%*) do echo%%i

To do a loop, it's also to show all the%1-%9 parameters.


Okay, now let's talk about that special%0.


%0 This is not a return parameter value, he has two layers of meaning!

First layer meaning: Returns the absolute path where the batch is processed

Example:
@echo off
Echo%0
Pause

Save as Test. BAT is run on the desktop and displays the following results
"C:/Documents and settings/administrator/desktop/test.bat"

He printed the path where the current batch was executed, which means returning the absolute path of the batch.

The second layer of meaning: infinite loop execution bat

Example:
@echo off
NET user
%0

Save as Bat execution, he will loop indefinitely to execute the net user command until you manually stop.

The above are some of the system variables in the batch, and there are some variables, they also represent some functions,
For those in the for command, the for variable has already been said and is not spoken.


Now say custom variables

So the name thinks, the custom variable is the variable that we give him the value

To use a custom variable you have to use the SET command, see examples.

@echo off
Set Var= I'm a value
Echo%var%
Pause

Save As Bat execution, we'll see that CMD returns a "I'm a Value"

var is the variable name, and the = number right is the value to give the variable
This is the simplest way to set a variable.

If we want the user to manually enter the value of the variable instead of specifying it in the code, you can use the/p parameter with the SET command

Example:

Copy Code code as follows:

@echo off
set/p var= Please enter the value of the variable
Echo%var%
Pause


the var variable name = number to the right is the prompt, not the value of the variable
The value of the variable is run by us with the keyboard input!

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.