DOS Batch Advanced Tutorial Chapter fourth variable _dos/bat in batch processing

Source: Internet
Author: User
Tags current time error code require

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

Let's now take a detailed explanation of these two variables!

First, 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. 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. The user home 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 home directory is specified in Local Users and groups.
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 that contains the Windows server operating system root (that is, the systems root directory).
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. For more information about the time command, 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

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-
%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

Notice the middle space and we'll see the result:

In contrast to the code,%1 is "I am the first parameter"%2 is "I am the second parameter"
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 going to return the value of all the arguments at once, without having to enter%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.

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

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 stop manually (CTRL + C stops).
Tornado added: In fact,%0 is the first parameter%1 before, and of course the batch file name (including the path).

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.


Two, custom variable

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 cloud-dwelling community
Echo%var%
Pause

Save for bat execution, we'll see CMD return a "I'm a cloud community"

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:

@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 variable value of the variable by our own keyboard input after running!

The use of set/p can be done through set/? View Help Files

The/p command-line switch allows variable values to be set to a single line of input entered by the user. Displays the specified promptstring before reading the input line. The promptstring can be empty.

The above content cloud Habitat Community Small series of close test, with map.

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.