Summary of variables in DOS batch file

Time:2019-10-29

The variables in batch processing can be divided into two categories: system variable and custom variable.

System variable:

Their values are automatically assigned by the system according to the pre-defined conditions, that is, these variable systems have defined values for them.
We don’t need to assign values to him. We just need to call and list them all!

%Allusersprofile% returns the location of the all users profile locally.
%Appdata% returns locally where the application stores data by default.
%CD% returns the current directory string locally.
%Cmdcmdline% returns the exact command line used to launch the current cmd.exe locally.
%Cmdexversion% system returns the version number of the current command handler extension.
%Computername% ‘system returns the name of the computer.
%The COMSPEC% system returns the exact path of the command line interpreter executable.
%Date%: the system returns the current date. Use the same format as the date / t command. Generated by cmd.exe. Of

For more information about the date command, see date.
%Errorlevel% ‘the system returns the error code of the previous command. Usually a non-zero value is used to indicate an error.
%Homedrive%? The system returns the local workstation drive letter connected to the user’s home directory. Set based on the home directory value. use

The 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’s home directory is in the

Users and groups.
%Homeshare% system returns the network path of the user’s shared home directory. Set based on the home directory value. User home directory is

Specified in local users and groups.
%Logonserver% ‘locally 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.
%OS% ‘system returns the operating system name. Windows 2000 shows that its operating system is Windows NT.
%Path% the system specifies the search path for the executable.
%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? Identifier% system returns processor description.
%The processor? Level%? System returns the model of the processor installed on the computer.
%Processor? Revision% system returns the version number of the processor.
%Prompt% returns the command prompt settings of the current interpreter locally. Generated by cmd.exe.
%Random% system returns any decimal number between 0 and 32767. Generated by cmd.exe.
%SystemDrive% the system returns the drive containing the windows server operating system root.
%The systemroot% system returns the location of the root of the windows server operating system.
%Temp% and% TMP% systems and users return the default temporary directory used by applications available to the currently logged on user.

Some applications require temp, while others require TMP.
%Time% the system returns the current time. Use the same format as the time / t command. Generated by cmd.exe. Of

For more information about the time command, see time.
%Userdomain% returns the name of the domain containing the user account locally.
%Username% returns the name of the currently logged in user locally.
%Userprofile% returns the location of the current user’s profile locally.
%Windir% system returns the location of the operating system directory.

With so many system variables, how do we know what their values are?
Enter echo% windir% in CMD
Windir variable name, not random!
This will show the value of a variable!

For example, if we want to copy files to the boot 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 should be enclosed in quotation marks

In addition, there are some system variables that represent a meaning or an operation!

They are% 0% 1% 2% 3% 4% 5… Up to% 9 there is another%*

%This one is a little special. It has several meanings. Let’s talk about% 1 -% 9 first.

%1 returns the first parameter of the batch
%2 returns the second parameter of the batch
%3 -% 9 push class based on this

Reverse batch parameters? How to return them?

Let’s take a look at this example. Save the following code as test.bat and put it on disk C.

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

Enter CMD, enter CD C:/
Then enter test.bat. I’m the first parameter. I’m the second parameter. I’m the third parameter. I’m the fourth parameter.

Pay attention to the gap in the middle, and we will see the result as follows:

I’m the first parameter. I’m the second parameter. I’m the third parameter. I’m the fourth parameter.
I’m the first parameter
I’m the second parameter.
I’m the third parameter.
I’m the fourth parameter.

Compared with the code,% 1 is that I am the first parameter% 2 is that I am the second parameter
How to understand it!

These% 1 and% 9 can enable batch processing to run with parameters, greatly improving the batch processing function!

There’s another% *. What is it? It’s not very useful, it’s just to return parameters, but it’s to return all parameters at once.

You do not need to enter% 1% 2 to determine one by one

Example
@echo off
echo %*

Also save as test.bat to Disk C
Enter CMD, enter CD C:/
Then enter 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 showed all the parameters at one time.

@echo off

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

Make a cycle to display all the parameters of% 1 -% 9

 

OK, let’s start with the special% 0

%0 is not the value of the return parameter. It has two meanings!

First level meaning: return to the absolute path of batch processing

Example:
@echo off
echo %0
pause

Save it as test.bat and run it on the desktop. The following results will be displayed
“C: / documents and settings / administrator / desktop / test. Bat”

He printed out the path of the current batch execution, which is to return the absolute path of the batch.

The second meaning: infinite loop execution bat

Example:
@echo off
net user
%0

Save it as bat and it will execute the command net user in infinite cycles until you stop it manually.

The above are some system variables in batch processing, and some other variables, which also represent some functions.
Those in the for command are, the for variable has already been said, not to be said.

Now let’s talk about custom variables.

So it’s called thinking. Custom variable is the variable that we give value to.

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

@echo off
Set var = I am the value
echo %var%
pause

Save as bat execution, we will see a “I am the value” returned in the 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 simplest 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:

Copy codeThe code is as follows:
@echo off
Set / P var = please enter the value of the variable
echo %var%
pause

To the right of VaR variable name = is the prompt, not the value of the variable.
The value of the variable is input by ourselves with keyboard after running!

Recommended Today

Custom factory class

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace LYZB.ApiConfig { /// ///Custom factory class /// // generic public abstract class AssesUtil where T : class,new() { /// ///Cache collection /// private static Hashtable hash = Hashtable.Synchronized(new Hashtable()); /// ///Execution instance method /// ///Method name ///Parameter type ///Parameter entity /// public […]