如何删除Wmic输出中的空行? [英] How can I remove empty lines from wmic output?

查看:385
本文介绍了如何删除Wmic输出中的空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我获取TeamViewer(任何版本)服务可执行文件位置的字符串:

This is my string to obtain the position of TeamViewer (any version) service executable:

for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname') do set POSITION=%A

此问题是由wmic引起的,因为它在结果末尾(在Windows 7命令上)包括一个空行,这是输出:

The problem is caused by wmic because it includes an empty line at the end of result (on Windows 7 command) and this is the output:

C:\Users\giovanni>for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%
'" get pathname') do set POSITION=%A

 :\Users\giovanni>set POSITION="C:\Program Files\TeamViewer\Version8\TeamViewer_Service.exe"

 :\Users\giovanni>set POSITION=

C:\Users\giovanni>echo %position%
ECHO enabled.

我怎样才能只获得输出的第二行以及正确的可执行文件位置? (或者跳过最后一行).

How I can get only the second line of the output with the correct position of the executable? (or skip the latest line, of course).

提前谢谢大家,祝您有个愉快的一天. 乔凡尼.

Thanks all in advance and have a nice day. Giovanni.

这是checktv.bat:

This is checktv.bat:

for /f "skip=1 delims=" %%A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"') do set POSITION=%%A
echo %POSITION%

推荐答案

像这样:

for /f "skip=1 delims=" %A in (
  'wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"'
) do set POSITION=%A

findstr /r /v "^$"从输出中删除空行.

这篇关于如何删除Wmic输出中的空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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