Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of variables in DOS batch processing

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the sample analysis of variables in DOS batch processing, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

I. system variables

Their values are automatically assigned by the system according to pre-defined conditions, that is, these variables have been defined by the system.

We don't need to assign a value to him, we just need to call it! I'll list them all!

% ALLUSERSPROFILE% returns the location of the all users profile locally.

% APPDATA% returns locally the location 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 locally.

The% CMDEXTVERSION% system returns the version number of the current Command processor extension.

The COMPUTERNAME% system returns the name of the computer.

The% COMSPEC% system returns the exact path to the command line interpreter executable.

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 returned the error code of the previous command. Errors are usually represented by a non-zero value.

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% returns locally the name of the domain controller that validates the current login 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 shows that its operating system is Windows_NT.

The PATH% system specifies the search path for executable files.

The% PATHEXT% system returns a list of file extensions that the operating system considers executable.

% PROCESSOR_ARCHITECTURE% system returns the chip architecture of the processor. Value: x86 or IA64 based on Itanium

% PROCESSOR_IDENTFIER% system returned processor description.

The PROCESSOR_LEVEL% system returns the model 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 number 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 system root).

The SYSTEMROOT% system returns the location of the Windows server operating system root directory.

The% TEMP% and% TMP% systems and users return the default temporary directory used by the applications available to the currently logged in user. Some applications require TEMP, while others 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% returns locally the name of the domain that contains the user account.

% USERNAME% returns the name of the currently logged in user locally.

% USERPROFILE% returns the location of the current user's profile locally.

The WINDIR% system returns the location of the operating system directory.

With so many system variables, how do we know what their value is?

Enter echo WINDIR% in CMD

So you can display the value of a variable!

For example, we can do this if we want to copy files to the startup directory of the current account.

Copy d:\ 1.bat "% USERPROFILE%\" start menu\ Program\ start\ "

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

There are also some system variables that represent the same meaning, or an operation!

They are 0% 1% 2% 3% 4% 5. Until% 9, there is still one% *.

% 0 is a bit special and has several layers of meaning. Let's first talk about the meaning of% 1% 9.

1 returns the first parameter of the batch

2 returns the second parameter of the batch

% 3mer% 9 infer according to this

Return batch parameters? How to return in the end?

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

@ echo offecho 1 2 3 4echo cho 2echo 3echo 4

Enter CMD and enter cd c:\

Then type test.bat. I'm the first parameter. I'm the second parameter. I'm the third parameter. I'm the fourth parameter.

Notice the space in the middle, and we will see the result like this:

Compared with the code, 1 is "I am the first parameter" and 2 is "I am the second parameter"

How do you understand?

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

There is another% * what is he? It's not very useful, it just returns parameters, but it returns all the parameters at once, and you don't have to enter% 1% 2 to determine one by one.

Examples

@ echo offecho% *

Also save as test.bat and put it on disk C.

Enter CMD and enter cd c:\

Then type test.bat. I'm the first parameter. I'm the second parameter. I'm the third parameter. I'm the fourth parameter.

You can see that he shows all the parameters at once.

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

% 0 this is not the value of the return parameter, it has two meanings!

First meaning: return to the absolute path where the batch is located

Example:

@ echo offecho% 0pause

Save it as test.BAT and run it on the desktop, and the result is as follows

He prints out the path where the current batch is executed, which means to return the absolute path where the batch is located

The second meaning: infinite loop execution of BAT

Example:

@ echo offnet user%0

Save as BAT, and it will loop through the net user command indefinitely until you stop it manually (ctrl+c stops).

Tornado added: in fact,% 0 is the parameter before the first parameter% 1, which is, of course, the batch file name (including path).

These are some of the system variables in the batch, and there are other variables that also represent some functions.

Those in the FOR command are those that have already been said by the FOR variable, so let's not talk about it.

II. Custom variables

Therefore, the name implies that a custom variable is a variable that we assign a value to it.

To use custom variables, you have to use the set command. Look at the example.

@ echo offset var=, this is echo var%pause.

Save as BAT execution, and we will see a "I am" returned in CMD.

Var is the variable name, and the value to be given to the variable is changed to the right of the = sign.

This is the easiest way to set variables.

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

Example:

@ echo offset / p var= Please enter the value of the variable echo var%pause

To the right of the var variable name = sign is the prompt, not the value of the variable. The value of the variable is entered by ourselves with the keyboard after we run it!

The use of set / p can be done through set /? View help files

The / P command line switch allows you to set the value of a variable to one line of input entered by the user. Displays the specified promptString before reading the input line. PromptString can be empty.

Thank you for reading this article carefully. I hope the article "sample Analysis of variables in DOS batches" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report