【问题标题】:How to use powershell pattern matching to find and run a .bat file如何使用 powershell 模式匹配来查找和运行 .bat 文件
【发布时间】:2017-11-25 10:59:33
【问题描述】:

我是一个 powershell 菜鸟,尽管我有谷歌搜索技能,但我一直无法弄清楚如何根据 powershell 中的模式找到文件的目录,然后运行该文件。

这些是要求...

  • 从特定文件夹 (C:\TFS) 开始
  • 在该文件夹中搜索名为“SpecFlow.bat”的特定批处理文件并返回文件夹和文件路径
  • 从上一步返回的目录运行文件

我有这样的事情,但我真的不知道我在做什么

Set-Location -Path C:\TFS
$a = Get-ChildItem C:\TFS -Filter SpecFlow.bat -Recurse | % { $_.FullName }
Start-Process $a

这似乎找到并运行文件,但在“C:\TFS”目录中。结果,我在我的 cmd 窗口中收到以下错误...

C:\TFS>SpecRun.exe run default.srprofile /basefolder:..\..\bin\release /outputfolder:output /report:MyReport.html
'SpecRun.exe' is not recognized as an internal or external command,
operable program or batch file.

我哪里错了?

对于奖励积分,我可以将我的批处理文件转换为所有 powershell 吗?批处理文件的内容是....

SpecRun.exe run default.srprofile /basefolder:..\..\bin\release /outputfolder:output /report:MyReport.html

暂停

与SpecRun.exe在同一目录下有依赖文件。

谢谢

【问题讨论】:

  • 如果SpecRun.exe 不在当前目录或%PATH% 中,您需要将该目录设为当前目录或包含其完整路径,而不仅仅是文件名。您似乎已经将C:\TFS 设为当前,(这似乎是您的问题),所以也许您想改用Set-Location -Path {C:\MyFolder}\packages\SpecRun.Runner.{version}\tools
  • @Compo。我正在寻找仅基于文件名的模式匹配来获取 SpecFlow.bat 文件的目录(我已将其放在与 SpecRun.exe 相同的目录中)。我可以专门导航到{C:\MyFolder}\packages\SpecRun.Runner.{version}\tools,但如果SpecRun.Runner.{version} 的版本发生变化怎么办?
  • Konzy262,那么将Set-Location 指向从您的Get-ChildItem 搜索中检索到的路径是否没有意义?此外,Start-Process 有一个 -WorkingDirectory 参数,可用于完全满足您的需求,(没有它的默认值是当前目录)。它还有一个可以使用的-ArgumentList 参数。

标签: windows powershell batch-file


【解决方案1】:

你可以这样做:

$file = Get-ChildItem -Recurse | Where-Object {$_.name -eq "SpecFlow.bat"}
cd $file.directory
StartProcess $file

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 2020-01-31
  • 2010-12-02
  • 2021-07-17
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多