Cmd 正在查找仅获取“的命令”;根路径“;

Cmd 正在查找仅获取“的命令”;根路径“;,cmd,Cmd,当我在命令提示符窗口中执行命令logman FabricTraces时,我会得到许多属性及其值的输出,如下所示: Name: FabricTraces Status: Running Root Path: C:\ProgramData\Windows Fabric\Fabric\log\Traces\ Segment: On Schedules: On Segment

当我在命令提示符窗口中执行命令
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
是否有任何命令仅获取
根路径

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%
的第一个
是获取所需字符串,第二个
是删除前导空格

根据您的评论,在
C#
中使用它需要一行程序:

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

根据您的评论,在
C#
中使用它需要一行程序:


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

logman-FabricTraces | findstr“根路径:”

-


来源:

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

logman-FabricTraces | findstr“根路径:”

-


来源:

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

窗口:

logman FabricTraces | findstr Root
Linux:

logman FabricTraces | grep Root

这应该只显示输出中的根路径行

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

窗口:

logman FabricTraces | findstr Root
Linux:

logman FabricTraces | grep Root

这应该只显示输出中的根路径行

这里还有一个解决方案:

@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/?
  • 获取/?
  • goto/?
  • 如果/?
  • logman/?
  • rem/?
  • 设置/?

另请阅读Microsoft关于的文章,了解
2>nul
的解释。当Windows命令解释器在执行的命令之前处理该命令行时,重定向操作符
必须在上用插入符号
^
转义,以便命令行被解释为文字字符,该命令行在单独的命令过程在后台启动。

这里还有一个解决方案:

@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/?
  • 获取/?
  • goto/?
  • 如果/?
  • logman/?
  • rem/?
  • 设置/?

另请阅读Microsoft关于的文章,了解
2>nul
的解释。当Windows命令解释器在执行的命令之前处理该命令行时,重定向操作符必须在上用插入符号^转义,以便命令行被解释为文字字符,该命令行在独立的命令过程在后台启动。

感谢Mofi编辑代码如果您需要C#解决方案(如答案注释中所述),您应该正确标记您的问题。既然已经有了有效的cmd解决方案,我建议再问一个问题。(你可以将这个问题与新问题链接起来。)感谢Mofi编辑代码。如果你需要C#解决方案(如回答评论中所述),你应该正确标记你的问题。既然已经有了有效的cmd解决方案,我建议再问一个问题。(您可以从新问题链接此问题。)注意:
findstr“根路径:
将返回包含
Root
和/或
Path:
的每一行(此特殊输出没有问题,但其他命令可能会产生意外结果)。要查找准确的字符串(包括空格
,请使用
findstr/c:“根路径:”
。您可能还需要
/b`开关。有关详细信息,请参阅
findstr/?
details@Stephan,有没有办法只获取“值”?@user584018:查看我的答案。结果分为多行。是否有方法在点击命令后获取结果注意:
findstr“Root Path:
将返回包含
Root
和/或
Path:
(此特殊输出没有问题,但其他命令可能会产生意外结果)。若要查找准确的字符串(包括空格
,请使用
findstr/c:“根路径:”
。您可能还需要
/b`开关。有关详细信息,请参阅
findstr/?
details@Stephan,有没有办法只获取“值”?@user584018:看我的答案。结果有多行。有没有办法在点击命令后得到结果谢谢,@Stephan,如果我们需要在C#中运行并将结果输出到变量中,如何做到这一点?请建议并帮助包括一行代码来输出值。如何在C#变量中获得结果是你关心的问题-我不知道C#。谢谢,@Stephan,我如果我们需要在C#中运行并将输出放入变量中,如何做到这一点?请建议并提供帮助,包括一个一行程序来输出值。如何将其放入C#变量是您关心的问题-我不知道C#。