China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-01 11:59
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » How can I understand and learn the set command well? View 1,206 Replies 6
Original Poster Posted 2007-02-07 10:38 ·  中国 山东 德州 电信
初级用户
Credits 24
Posts 11
Joined 2007-01-13 05:26
19-year member
UID 76357
Gender Male
Status Offline
Recently I've been learning batch files and downloaded some tutorials from the forum as well as some classic posts. I've gained a certain understanding of commands like if, for, call, etc., but set is the only one I still don't understand very deeply. I've read some posts and its built-in help, but still can't understand it very well. Could some experts please post something about set, combining it with some typical examples and giving a detailed explanation, explaining it line by line? I think that works pretty well, just like that post that gave some examples of for usage. Looking at the batch files written by the experts, I can't understand a lot of them. Most of it is in very small details, for example, for some formats I don't understand why they are written that way; for the use and combination of some symbols I don't understand the reasoning behind them. Many experts have provided very good batch file examples, but they didn't give detailed explanations, and for us newbies that's a big difficulty. If someone could, in spite of their busy schedule, write some good examples and choose one or two of them to explain in detail, then for us newbies the efficiency of learning batch programming would be much higher. I wonder whether what I'm saying makes sense. Sorry to trouble you!
Floor 2 Posted 2007-03-09 03:07 ·  中国 湖北 黄冈 电信
高级用户
★★★
Credits 894
Posts 411
Joined 2007-02-17 12:15
19-year member
UID 79697
Gender Male
Status Offline
I don't completely understand the set command either, so I can't really help much.
@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul


Floor 3 Posted 2007-03-09 03:29 ·  中国 广东 深圳 电信
新手上路
Credits 10
Posts 5
Joined 2006-01-18 02:19
20-year member
UID 49153
Status Offline
Set displays, sets, or deletes environment variables. If used without any parameters, the set command displays the current environment settings.

Syntax
set ] ] string]

Parameters
/a
Sets string to a numeric expression that can be evaluated.
/p
Sets the value of variable to a line of input.
variable
Specifies the variable to set or modify.
string
Specifies the string to associate with the specified variable.
/?
Displays help at the command prompt.
Remarks
Using set in the Recovery Console
The set command has different parameters when used from the Recovery Console.

Using special characters
The characters < > | & ^ are special command shell characters. When used in a string, they must be preceded by the escape character (^) or enclosed in quotation marks (that is, "StringContaining&Symbol"). If you use quotation marks to enclose a string containing special characters, those quotation marks become part of the environment variable value.

Using environment variables
Environment variables can be used to control the operation of certain batch files and programs, and can also control the way the Windows XP and MS-DOS subsystems display and work. The set command is often used in the Autoexec.nt file to set environment variables.

Displaying the current environment settings
If you type only the set command, the current environment settings are displayed. These settings usually include the COMSPEC and PATH environment variables, which help locate programs on the disk. Two other environment variables used by Windows XP are PROMPT and DIRCMD.

Using parameters
When you specify a variable and a string value, the specified variable value is added to the environment, and the string is associated with that variable. If the variable already exists in the environment, the new string value replaces the old string value.

If you specify only a variable and an equal sign for the set command (with no string), the string value associated with that variable is cleared (as if the variable did not exist at all).

Using /a
The following table lists the operators supported by /a in order of precedence from highest to lowest.

Operator Operation performed
< > Grouping
* / % + - Arithmetic
<< >> Logical shift
& Bitwise "and"
^ Bitwise "exclusive or"
| Bitwise "or"
= *= /= %= += -= &= ^= |= <<= >>= Assignment
, Expression separator

If you use logical (&& ||) or modulus (%) operators, enclose the expression string in quotation marks. Nonnumeric strings in expressions are treated as environment variable names, whose values are converted to numbers before processing. If an environment variable name is specified that is not defined in the current environment, a value of zero is assigned, allowing you to perform arithmetic with environment variables without having to use % to retrieve the value.

If you run set /a from the command line outside a command script, the final value of the expression is displayed.

Numeric values are decimal numbers. Prefixing with 0× indicates a hexadecimal number, and prefixing with 0 indicates an octal number. Therefore, 0×2 is the same as 18, and also the same as 022. Octal notation can easily cause confusion. For example, 08 and 09 are not valid numbers, because 8 and 9 are not valid octal digits.

Using /p
Requires a prompt string.

Supporting delayed environment variable expansion
Support for delayed environment variable expansion has been added. This support is disabled by default, but can be enabled or disabled with cmd /v.

Using command extensions
When command extensions are enabled (the default) and set is run by itself, all current environment variables are displayed. If you run set with a value, variables matching that value are displayed.

Using set in batch files
When creating a batch file, you can use the set command to create variables, and then use those variables the same way you use the numbered variables %0 through %9. You can also use the %0 through %9 variables as input to the set command.

Calling set variables from batch files.
When calling a variable value from a batch file, you must enclose the value in percent signs (%). For example, if a batch program creates an environment variable BAUD, you can type %baud% at the command line and use the string associated with BAUD as a replaceable parameter.

Examples
To set an environment variable named TEST^1, type:

set testVar=test^^1

To set an environment variable named TEST&1, type:

set testVar=test^&1

Set assigns the variable value to the content after the equal sign (=). If you type:

set testVar="test^1"

the result will be:
testVar="test^1"

To set an environment variable named INCLUDE so that the string C:\Inc( (the \Inc directory on drive C) is associated with that variable, type:

set include=c:\inc

You can then use the string C:\Inc in a batch file by enclosing INCLUDE in percent signs (%). For example, you might include the following command in a batch file to display the contents of the directory associated with the INCLUDE environment variable.

dir %include%

When this command is processed, the string C:\Inc replaces %include%.

You can also use set in a batch program to add a new directory to the PATH environment variable. For example:

@echo off
rem ADDPATH.BAT adds a new directory
rem to the path environment variable.
set path=%1;%path%
set

When command extensions are enabled (the default) and set is run with a value, variables matching that value are displayed. For example, if you type set p at the command prompt, the result will be:

Path=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 8 Stepping 1, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0801
ProgramFiles=C:\Program Files
PROMPT=$P$G
Floor 4 Posted 2007-03-09 07:33 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
The basic usage is what set's help says, but online you'll see a lot of unusual usages; they're just flexible variations of some of those basic usages. Analyze them bit by bit yourself and you'll understand.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 5 Posted 2007-03-11 03:07 ·  中国 江苏 连云港 电信
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Yes, exactly. I can understand how everyone feels. I also got here by searching little by little, reading while thinking, and thinking while practicing. That way, most problems can be figured out by yourself. If you really don't understand something, then ask. I think 99 percent of problems can be solved.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 6 Posted 2007-06-01 15:40 ·  中国 北京 海淀区 IDC机房
初级用户
Credits 50
Posts 19
Joined 2007-05-22 21:13
19-year member
UID 89005
Gender Male
Status Offline
I still don't understand the meaning of variables and expanded variables
Floor 7 Posted 2007-06-02 04:42 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Forum Jump: