Windows CMD FOR 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12724123/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:53:50  来源:igfitidea点击:

Windows CMD FOR loop

windowsbatch-filecmd

提问by michaeluskov

I'm trying to make a code which will get first words from all lines of HELP's output to a variable and echo this variable. Here is my code:

我正在尝试编写一个代码,它将从 HELP 输出的所有行中获取第一个单词到一个变量并回显该变量。这是我的代码:

@echo off
set a=
for /F "tokens=1,*" %%i in ('help') do (
set a=%a% %%i 
)
echo %a%

But it returns first word from only last line. Why?

但它只从最后一行返回第一个单词。为什么?

回答by dbenham

Bali C solved your problem as stated, but it looks to me like you are trying to get a list of commands found in HELP.

Bali C 如上所述解决了您的问题,但在我看来,您正在尝试获取在 HELP 中找到的命令列表。

Some of the commands appear on multiple lines, so you get some extraneous words. Also there is a leading and trailing line beginning with "For" on an English machine that is not wanted.

一些命令出现在多行中,因此您会得到一些无关的词。在英语机器上还有一个以“For”开头的前导和尾随行,这是不需要的。

Here is a short script for an English machine that will build a list of commands. The FINDSTR command will have to change for different languages.

这是一个用于构建命令列表的英文机器的简短脚本。FINDSTR 命令必须针对不同的语言进行更改。

@echo off
setlocal enableDelayedExpansion
set "cmds="
for /f "eol= delims=." %%A in ('help^|findstr /bv "For"') do (
  for /f %%B in ("%%A") do set "cmds=!cmds! %%B"
)
set "cmds=%cmds:~1%"
echo %cmds%


EDIT


编辑

Ansgar Wiechers came up with a more efficient algorithm to extract just the command names at https://stackoverflow.com/a/12733642/1012053that I believe should work with all languages. I've used his idea to simplify the code below.

Ansgar Wiechers 提出了一种更有效的算法来仅提取https://stackoverflow.com/a/12733642/1012053上的命令名称,我认为它适用于所有语言。我用他的想法来简化下面的代码。

@echo off
setlocal enableDelayedExpansion
set "cmds="
for /f %%A in ('help^|findstr /brc:"[A-Z][A-Z]*  "') do set "cmds=!cmds! %%A"
set "cmds=%cmds:~1%"
echo %cmds%

回答by Bali C

You need to use delayed expansion in your forloop

您需要在for循环中使用延迟扩展

@echo off
setlocal enabledelayedexpansion
set a=
for /F "tokens=1,*" %%i in ('help') do (
set a=!a! %%i 
)
echo %a%

Instead of using %'s around the avariable, you use !'s to use delayed expansion.

不是%a变量周围使用's ,而是使用!'s 来使用延迟扩展。

回答by Steve

Because the echo is outside the do ( ...... )

因为回声在 do ( ...... ) 之外

@echo off
for /F "tokens=1,*" %%i in ('help') do (
echo %%i
)

and no need to print a, you can use directly %%i.
Another very simple example could be a batch like this saved as help1.cmd

并且不需要打印a,直接使用%%i即可。
另一个非常简单的例子可能是这样的批处理,保存为 help1.cmd

@echo off
for /F "tokens=1,*" %%i in ('help') do (
if /I "%%i" EQU "%1" echo %%j
)

and you call this batch like

你把这批叫做

help1 MKDIR 

to get the short help text for the MKDIR command

获取 MKDIR 命令的简短帮助文本