如何使用powershell模式匹配来查找并运行.bat文件
问题描述:
我是powershell noob,尽管使用了Google的技巧我一直无法找到如何根据PowerShell中的模式找到文件的目录,然后运行该文件。如何使用powershell模式匹配来查找并运行.bat文件
这些要求......
- 开始在一个特定的文件夹(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相关文件。
感谢
答
你可以这样做:
$file = Get-ChildItem -Recurse | Where-Object {$_.name -eq "SpecFlow.bat"}
cd $file.directory
StartProcess $file
+0
虽然此代码片段可能会解决问题,但[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – Clijsters
如果'SpecRun.exe'不在当前目录或'%PATH%'你需要做的是当前目录或包含它是完整的路径,而不仅仅是文件名。你已经看起来正在使'C:\ TFS'成为最新的_(这就是你所说的是你的问题)_,所以也许你想使用'Set-Location -Path {C:\ MyFolder} \ packages \ SpecRun.Runner。{version} \ tools'。 – Compo
@Compo。我正在寻找一个SpecFlow.bat文件的目录(我把它放在与SpecRun.exe相同的目录中),仅基于文件名的模式匹配。我可以专门导航到'{C:\ MyFolder} \ packages \ SpecRun.Runner。{version} \ tools',但是如果“SpecRun.Runner。{version}”的版本会发生变化呢? – Konzy262
Konzy262,从'Get-ChildItem'搜索中将'Set-Location'设置为检索路径吗?另外,'Start-Process'有一个'-WorkingDirectory'参数,它可以用来完成你所需要的,_(默认没有它就是当前目录)_。它也有一个'-ArgumentList'参数可以使用。 – Compo