[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转贴] 国外网站的 Set 教程

转自:http://www.robvanderwoude.com/ntset.php,格式有变,建议在原地址阅读,请自行参考谷歌翻译,话说这个网站好东西很多嘿
其实我想说的是,好东西在后面...
-------------------------- 以下是转载 ----------------------------

SET

Windows NT 4..Windows 7 Syntax

Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

variable        Specifies the environment-variable name.
string        Specifies a series of characters to assign to the variable.
Type SET without parameters to display the current environment variables.

If Command Extensions are enabled SET changes as follows:

SET command invoked with just a variable name, no equal sign or value will display the value of all variables whose prefix matches the name given to the SET command. For example:

SET P
would display all variables that begin with the letter 'P'

SET command will set the ERRORLEVEL to 1 if the variable name is not found in the current environment.

SET command will not allow an equal sign (=) to be part of the name of a variable (with the exception of some dynamic environment variables).
However, SET command will allow an equal sign in the value of an environment variable in any position other than the first character.

Integer Math

The /A switch was introduced in Windows NT 4:

SET /A expression
The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence:

()        grouping
* / %        arithmetic operators
+ -        arithmetic operators
<< >>        logical shift
&        bitwise and
ˆ        bitwise exclusive or
|        bitwise or
= *= /= %= += -=
&= ˆ= |= <<= >>=        assignment
,        expression separator
If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes.
Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them.
If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.
If SET /A is executed from the command line outside of a command script, then it displays the final value of the expression.
The assignment operator requires an environment variable name to the left of the assignment operator.
Numeric values are decimal numbers, unless prefixed by 0x for hexidecimal numbers, 0b for binary numbers and 0 for octals numbers. So 0x12 is the same as 0b10010 is the same as 022 is the same as 18.
Please note that the octal notation can be confusing: 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.

Note:        A note on NT 4's SET /A switch from Walter Zackery in a message on alt.msdos.batch.nt:
"The SET /A command has a long list of problems.
I wouldn't use it for much more than simple arithmetic, although even then it truncates all answers to integers."

On the other hand, limited though it may seem, the SET command's math function can even be used for a complex task like calculating the date of Easter Day for any year.
But always keep in mind that it is limited to 16- or 32-bit integers, depending on the Windows version.


More on integer math, and its limitations, can be found on my Batch math and PHP based batch files pages.

Prompt For Input

The /P switch was introduced in Windows 2000:

SET /P variable=[promptString]
The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

String Substitution

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%
would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the begining of the expanded output to the first occurrence of the remaining portion of str1.

Note:        I'm not sure about other Windows versions, but in Windows XP and Windows 7 string substitution is case-insensitive, e.g. SET Var=%Var:A=A% will convert all occurrences of the letter a in variable Var into upper case As (tip by Brad Thone & Brian Williams).
Substrings

May also specify substrings for an expansion.

%PATH:~10,5%
would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result.
If the length is not specified, then it defaults to the remainder of the variable value.
As of Windows 2000, if either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified.

%PATH:~-10%
would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%
would extract all but the last 2 characters of the PATH variable.

Finally, support for delayed environment variable expansion has been introduced in Windows 2000. This support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

Delayed Variable Expansion

Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed.
The following example demonstrates the problem with immediate variable expansion:
  1. SET VAR=before
  2. IF "%VAR%" == "before" (
  3.     SET VAR=after;
  4.     IF "%VAR%" == "after" @ECHO If you see this, it worked
  5. )
复制代码
would never display the message, since the %VAR% in BOTH IF statements is substituted when the first IF statement is read, since it logically includes the body of the IF, which is a compound statement.
So the IF inside the compound statement is really comparing "before" with "after" which will never be equal.
Similarly, the following example will not work as expected:
  1. SET LIST=
  2. FOR %A IN (*) DO SET LIST=%LIST% %A
  3. ECHO %LIST%
复制代码
in that it will NOT build up a list of files in the current directory, but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:
  1. FOR %A IN (*) DO SET LIST= %A
复制代码
which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time.
If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:
  1. SET VAR=before
  2. IF "%VAR%" == "before" (
  3.     SET VAR=after
  4.     IF "!VAR!" == "after" @ECHO If you see this, it worked
  5. )
  6. SET LIST=
  7. FOR %A IN (*) DO SET LIST=!LIST! %A
  8. ECHO %LIST%
复制代码
Dynamic Environment Variables

As of Windows 2000, if Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET.
These variable values are computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below:

%=C:%        expands to the current directory string on the C: drive
%=D:%        expands to the current directory string on the D: drive if drive D: has been accessed in the current CMD session
%=ExitCode%        expands to the hexadecimal value of the last return code set by EXIT /B
%=ExitCodeAscii%        expands to the ASCII value of the last return code set by EXIT /B if greater than 32 (decimal)
%CD%        expands to the current directory string (no trailing backslash, unless the current directory is a root directory)
%__CD__%        expands to the current directory string, always terminated with a trailing backslash
%DATE%        expands to current date using same format as DATE command
%TIME%        expands to current time using same format as TIME command
%RANDOM%        expands to a random decimal number between 0 and 32767
%ERRORLEVEL%        expands to the current ERRORLEVEL value
%CMDEXTVERSION%        expands to the current Command Processor Extensions version number
%CMDCMDLINE%        expands to the original command line that invoked the Command Processor


Notes:        1        The first 4 dynamic variables listed (the ones starting with the equal sign) were discovered by "SmartGenius".
The current values are revealed using the command SET ""
I do not know if these variables are available in Windows 2000.
"SmartGenius" supplied the following demo script:
  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. FOR %%A IN (83 109 97 114 116 71 101 110 105 117 115) DO (
  4.     CMD /C EXIT /B %%A
  5.     SET /P "Var=!=ExitCodeAscii!" < NUL
  6. )
  7. ECHO.
  8. ENDLOCAL
复制代码
Try it, it is harmless...
        2        The following sample code, by Thomas Freundt, lists all %=D:% like variables:
  1. FOR /F "tokens=* delims==" %%A IN ('SET "" ˆ| FINDSTR.EXE /B "="') DO @ECHO.%%A
复制代码
Or a simplified version for the command line, if you don't mind a more cluttered output:
  1. SET "" | FINDSTR.EXE /B "="
复制代码
3
  1. If %=D:% is not available (not verified for Windows 2000 or NT 4)
复制代码
it can be emulated by the following code:
  1. PUSHD D:
  2. SET CDD=%CD%
  3. POPD
复制代码
4        The dynamic variable %__CD__% was discovered by Menno Vogels.
I do not know if this variables is available in Windows 2000.
2

评分人数

竟然还有这个 %__CD__%  ,很好。。

TOP

很好很强大,, 关于ascii码转字符的
非常批处理3群:56794763
有偿写程序.
批处理, lua, c, c++, vba, php, css

TOP

返回列表