寻找命令只获得“根路径”

时间:2017-11-20 08:06:11

标签: cmd

当我在命令提示符窗口中执行命令logman FabricTraces时,我会输出许多属性及其值,如下所示:

Name:                 FabricTraces
Status:               Running
Root Path:            C:\ProgramData\Windows Fabric\Fabric\log\Traces\
Segment:              On
Schedules:            On
Segment Max Size:     128 MB
Run as:               SYSTEM

是否有任何命令只能获得Root Path值?

4 个答案:

答案 0 :(得分:2)

for /f "tokens=1* delims=:" %%a in ('logman FabricTraces^|find "Root Path"') do for /f "tokens=*" %%r in ("%%b") do set rootpath=%%r
echo %rootpath%

第一个for是获取所需的字符串,第二个for是删除前导空格。

根据您的评论,您需要使用单行代码才能在C#中使用

cmd /c "@for /f "tokens=1* delims=:" %a in ('logman FabricTraces^|find "Root Path"') do @for /f "tokens=*" %r in ("%b") do @echo %r "

答案 1 :(得分:2)

您可以使用grep在Windows中获得findstr实用程序的类似效果:

logman FabricTraces | findstr "Root Path:"

-

来源:https://www.mkyong.com/linux/grep-for-windows-findstr-example/

答案 2 :(得分:1)

管道输出和选项?如果是这样,您可以在Windows中使用findstr,如果它是Linux基本系统,则可以使用grep

视窗:

logman FabricTraces | findstr Root

Linux的:

logman FabricTraces | grep Root

这应该只显示输出中的Root Path行。

答案 3 :(得分:0)

这是另一个解决方案:

@echo off
for /F "tokens=1,2*" %%A in ('%SystemRoot%\System32\logman.exe FabricTraces 2^>nul') do if "%%A %%B" == "Root Path:" set "RootPath=%%C" & goto RootPath
echo Failed to get root path for FabricTraces.
goto :EOF

:RootPath
echo Root path for FabricTraces is: %RootPath%
rem More commands using environment variable RootPath.

使用:作为分隔符而不是默认选项卡和空格进行了一些优化:

@echo off
for /F "tokens=1* delims=:" %%A in ('%SystemRoot%\System32\logman.exe FabricTraces 2^>nul') do if "%%A" == "Root Path" set "RootPath=%%B" & goto RootPath
echo Failed to get root path for FabricTraces.
goto :EOF

:RootPath
echo Root path for FabricTraces is: %RootPath%
rem More commands using environment variable RootPath.

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • for /?
  • goto /?
  • if /?
  • logman /?
  • rem /?
  • set /?

另请阅读有关Using Command Redirection Operators的Microsoft文章,了解2>nul的说明。重定向运算符>必须使用 FOR 命令行上的插入符^进行转义,以便在执行命令之前Windows命令解释程序处理此命令行时将其解释为文字字符FOR ,它在后台启动的单独命令进程中执行嵌入式logman.exe命令行。