在批处理文件中存储命令的返回值 [英] Storing return value of a command in batch file

查看:72
本文介绍了在批处理文件中存储命令的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个批处理文件,我想在其中存储以下命令(在Windows中)返回的值:

I am creating a batch file, in which I want to store the value returned by the following command (in Windows):

netstat -an | find ":port" /c

如何存储计数值并使用回显进行打印?

How to store the count value and print using echo?

推荐答案

要捕获命令的输出,可以使用for /F命令.要将其存储到变量中,请在for循环的正文中使用set:

To capture the output of a command, you can use the for /F command; to store it into a variable, use set in the body of the for loop:

for /F "delims=" %L in ('netstat -an ^| find ":port" /c') do (set "VAR=%L")

注意逸出的管道^|.要在批处理文件中使用此文件,请将%L替换为%%L.

Note the escaped pipe ^|. To use this within a batch file, replace %L by %%L.

这仅适用于单行输出.如果命令返回多行输出,则仅最后一行存储在变量VAR中.

This only works for a single-line output. If a command returns a multi-line output, only the last line is stored in variable VAR.

这篇关于在批处理文件中存储命令的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆