![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-18 22:13 |
47,814 topics / 349,908 posts / today 1 new / 48,259 members |
| DOS疑难解答 & 问题讨论 (解答室) » How can I understand and learn the set command well? |
| Printable Version 1,240 / 6 |
| Floor1 sjjhhx | Posted 2007-02-07 10:38 |
| 初级用户 Posts 11 Credits 24 | |
|
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!
|
|
| Floor2 6622186 | Posted 2007-03-09 03:07 |
| 高级用户 Posts 411 Credits 894 | |
|
I don't completely understand the set command either, so I can't really help much.
|
|
| Floor3 milini | Posted 2007-03-09 03:29 |
| 新手上路 Posts 5 Credits 10 | |
|
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 |
|
| Floor4 Climbing | Posted 2007-03-09 07:33 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
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.
|
|
| Floor5 oilio | Posted 2007-03-11 03:07 |
| 高级用户 Posts 303 Credits 641 | |
|
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.
|
|
| Floor6 371770923 | Posted 2007-06-01 15:40 |
| 初级用户 Posts 19 Credits 50 | |
|
I still don't understand the meaning of variables and expanded variables
|
|
| Floor7 lxmxn | Posted 2007-06-02 04:42 |
| 版主 Posts 4,938 Credits 11,386 | |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |