简体   繁体   中英

How to get output of a command only when it is a single line of text?

In a batch file, I am using the following to get the ouput of a command :

for /f %%i in ('command') do set output=%%i

command is supposed to output a single line of text, and I get the expected output in %output% in such a case.

However, command sometimes outputs several lines of text, which represents an error. In such a situation, I get the last one in %output% and I don't know that command did output more than a single line.

How to check when command outputs more than one single line of text ?

Note: I don't need to know what the outputs are when there are several lines; I only need to known that more than a single line was output.

set "output="
for /f %%i in ('command') do if defined output (
echo "error"
) else (
set output=%%i
)

Using the fact that if defined interprets the run-time status of the target variable

…or maybe

for /f %%i in ('command 2^>nul') do set output=%%i

it's hard to say when we don't know what command or error output is

cmd.exe /k "for /f %%i in ('command') do set output=%%i" > C:/Users/%username%/Desktop/Output.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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