Storing return value of a command in batch file

半腔热情 提交于 2020-03-16 09:21:08

问题


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?


回答1:


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")

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

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.




回答2:


I would guess something similar to:

@echo off
set "Blah=netstat -an|find ":port" /c"
echo %Blah%
pause


来源:https://stackoverflow.com/questions/33338817/storing-return-value-of-a-command-in-batch-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!