简体   繁体   English

如何将批处理(CALL)命令的输出存储到变量

[英]How to store the output of batch (CALL) command to a variable

I have the batch file which has the command to call second.bat file. 我有批处理文件,该文件具有调用second.bat文件的命令。 It will produce single line of output when it called. 调用时将产生单行输出。 I want to store that line into variable. 我想将该行存储到变量中。

CALL second.bat

I have tried using the following lines of commands but no use 我已经尝试过使用以下几行命令,但没有用

FOR /F "tokens=* USEBACKQ" %%F IN ('COMMAND') do SET result=%%F
FOR /F "tokens=1 delims= " %%A IN ('COMMAND') DO SET NumDocs=%%A

I don't know what to replace with COMMAND 我不知道该用COMMAND替换什么

As the help will tell you, COMMAND should be the command you want to run and get the output of. 正如帮助将告诉您的那样, COMMAND应该是您要运行并获取其输出的命令。 So in your case second.bat . 因此,在您的情况下second.bat At least it works for me: 至少对我有用:

@echo off
FOR /F "tokens=*" %%F IN ('second.bat') do SET result=%%F
echo %result%

Note that you cannot use the usebackq option if you're using ' to delimit your command. 请注意,如果使用'来分隔命令,则不能使用usebackq选项。

tl;dr TL;博士

To complement Joey's helpful answer (which fixes the problem with your 1st command) with a fixed version of both your commands: 为了用两个命令的固定版本来补充Joey的有用答案 (它解决了第一个命令的问题):

::  'usebackq' requires enclosing the command in `...` (back quotes aka backticks)
FOR /F "tokens=* usebackq" %%F IN (`second.bat`) do SET result=%%F

:: No characters must follow "delims="; 'tokens=1' is redundant
FOR /F "delims="           %%F IN ('second.bat') DO SET result=%%F

Note that in this case there's no need for call in order to invoke second.bat (which you normally need in order to continue execution of the calling batch file), because any command inside (...) is run in a child cmd.exe process. 请注意,在这种情况下,无需call即可调用second.bat (通常需要该文​​件才能继续执行调用批处理文件),因为(...)任何命令都在 cmd.exe运行cmd.exe程序。


The only thing needed to make your 2nd command work is to remove the space after delims= : 使您的第二个命令起作用的唯一需要做的就是delims=之后删除delims=

FOR /F "tokens=1 delims=" %%F IN ('second.bat') DO SET result=%%F

delims= - ie, specifying no delimiters (separators) - must be placed at the very end of the options string , because the very next character is invariably interpreted as a delimiter, which is what happened in your case: a space became the delimiter. delims= -即指定任何分隔符(分隔符) - 必须放置选项字符串的结尾处 ,因为第二天字符总是解释为一个分隔符,这是你的情况发生了什么:一个空间成了分隔符。

Also, you can simplify the command by removing tokens=1 , because with delims= you by definition only get 1 token (per line) , namely the entire input (line), as-is: 另外,您可以通过删除tokens=1简化命令,因为按照定义delims=您仅获得1个令牌(每行) ,即按原样输入整个输入(行):

FOR /F "delims=" %%F IN ('second.bat') DO SET result=%%F

Finally, it's worth noting that there's a subtle difference between tokens=* and delims= : 最后,值得注意的是, tokens=*delims=之间存在细微的差别

  • delims= (at the very end of the options string) returns the input / each input line as-is . delims= (在选项字符串的最后) 按原样返回输入/每个输入行。

  • tokens=* strips leading delimiter instances from the input; tokens=*从输入中删除前导定界符实例 with the default set of delimiters - tabs and spaces - leading whitespace is trimmed. 使用默认的定界符集-制表符和空格-裁剪前导空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM