Windows 批处理文件的隐藏功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/245395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hidden features of Windows batch files
提问by Chris Noe
What are some of the lesser know, but important and useful features of Windows batch files?
Windows 批处理文件有哪些鲜为人知但重要且有用的功能?
Guidelines:
准则:
- One feature per answer
- Give both a short descriptionof the feature and an example, not just a link to documentation
- Limit answers to native funtionality, i.e., does not require additional software, like the Windows Resource Kit
- 每个答案一个功能
- 提供功能的简短描述和示例,而不仅仅是文档链接
- 限制对本机功能的回答,即不需要额外的软件,如Windows Resource Kit
Clarification: We refer here to scripts that are processed by cmd.exe, which is the default on WinNT variants.
说明:我们在这里指的是由 cmd.exe 处理的脚本,这是 WinNT 变体的默认设置。
(See also: Windows batch files: .bat vs .cmd?)
(另请参阅:Windows 批处理文件:.bat 与 .cmd?)
采纳答案by Chris Noe
Line continuation:
线路延续:
call C:\WINDOWS\system32\ntbackup.exe ^
backup ^
/V:yes ^
/R:no ^
/RS:no ^
/HC:off ^
/M normal ^
/L:s ^
@daily.bks ^
/F daily.bkf
回答by raven
PUSHD path
Takes you to the directory specified by path.
将您带到path指定的目录。
POPD
Takes you back to the directory you "pushed" from.
带您回到您“推送”的目录。
回答by LeopardSkinPillBoxHat
Not sure how useful this would be in a batchfile, but it's a very convenient command to use in the command prompt:
不确定这在批处理文件中会有多大用处,但在命令提示符中使用它是一个非常方便的命令:
C:\some_directory> start .
This will open up Windows Explorer in the "some_directory" folder.
这将在“some_directory”文件夹中打开 Windows 资源管理器。
I have found this a great time-saver.
我发现这是一个很好的节省时间的方法。
回答by Chris Noe
I have always found it difficult to read comments that are marked by a keyword on each line:
我总是发现很难阅读每行用关键字标记的评论:
REM blah blah blah
Easier to read:
更容易阅读:
:: blah blah blah
回答by Chris Noe
Variable substrings:
可变子串:
> set str=0123456789
> echo %str:~0,5%
01234
> echo %str:~-5,5%
56789
> echo %str:~3,-3%
3456
回答by TheSoftwareJedi
The FOR command! While I hate writing batch files, I'm thankful for it.
FOR 命令!虽然我讨厌编写批处理文件,但我对此表示感谢。
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd.
将解析 myfile.txt 中的每一行,忽略以分号开头的行,将每行的第二个和第三个标记传递给 for 正文,标记以逗号和/或空格分隔。注意 for body 语句引用 %i 获取第二个标记,%j 获取第三个标记,以及 %k 获取第三个之后的所有剩余标记。
You can also use this to iterate over directories, directory contents, etc...
您还可以使用它来迭代目录、目录内容等...
回答by Patrick Cuff
Rather than litter a script with REM or :: lines, I do the following at the top of each script:
我没有用 REM 或 :: 行乱扔脚本,而是在每个脚本的顶部执行以下操作:
@echo OFF
goto :START
Description of the script.
Usage:
myscript -parm1|parm2 > result.txt
:START
Note how you can use the pipe and redirection characters without escaping them.
请注意如何使用管道和重定向字符而不转义它们。
回答by RealHowTo
The path (with drive) where the script is : ~dp0
脚本所在的路径(带驱动器):~dp0
set BAT_HOME=%~dp0
echo %BAT_HOME%
cd %BAT_HOME%
回答by RealHowTo
The %~dp0 piece was mentioned already, but there is actually more to it:
the character(s) after the ~ define the information that is extracted.
No letter result in the return of the patch file name
d - returns the drive letter
p - returns the path
s - returns the short path
x - returns the file extension
So if you execute the script test.bat below from the c:\Temp\long dir name\ folder,
已经提到了 %~dp0 部分,但实际上还有更多内容: ~ 后面的字符定义了提取的信息。
没有字母结果返回补丁文件名
d - 返回驱动器号
p - 返回路径
s - 返回短路径
x - 返回文件扩展名
所以如果你从 c:\Temp 下面执行脚本 test.bat \长目录名称\文件夹,
@echo off
echo %0
echo %~d0
echo %~p0
echo %~dp0
echo %~x0
echo %~s0
echo %~sp0
you get the following output
你得到以下输出
test c: \Temp\long dir name\ c:\Temp\long dir name\ .bat c:\Temp\LONGDI~1\test.bat \Temp\LONGDI~1\
test c: \Temp\long dir name\ c:\Temp\long dir name\ .bat c:\Temp\LONGDI~1\test.bat \Temp\LONGDI~1\
And if a parameter is passed into your script as in
test c:\temp\mysrc\test.cpp
the same manipulations can be done with the %1 variable.
如果在
测试 c:\temp\mysrc\test.cpp
中将参数传递到您的脚本中,则可以使用 %1 变量完成相同的操作。
But the result of the expansion of %0 depends on the location!
At the "top level" of the batch it expands to the current batch filename.
In a function (call), it expands to the function name.
但是 %0 扩展的结果取决于位置!
在批处理的“顶层”,它扩展为当前批处理文件名。
在函数(调用)中,它扩展为函数名。
@echo off
echo %0
call :test
goto :eof
:test
echo %0
echo %~0
echo %~n0
The output is (the batchfile is started with myBatch.bat )
输出是(批处理文件以 myBatch.bat 开始)
myBatch.bat
:test
:test
myBatch
回答by Ferruccio
By using CALL, EXIT /B, SETLOCAL & ENDLOCAL you can implement subroutines with local variables.
通过使用 CALL、EXIT /B、SETLOCAL 和 ENDLOCAL,您可以使用局部变量实现子例程。
example:
例子:
@echo off
set x=xxxxx
call :sub 10
echo %x%
exit /b
:sub
setlocal
set /a x=%1 + 1
echo %x%
endlocal
exit /b
This will print
这将打印
11
xxxxx
even though :sub modifies x.
即使 :sub 修改了 x。