2017-11-25 194 views
1

我是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相關文件。

感謝

+0

如果'SpecRun.exe'不在當前目錄或'%PATH%'你需要做的是當前目錄或包含它是完整的路徑,而不僅僅是文件名。你已經看起來正在使'C:\ TFS'成爲最新的_(這就是你所說的是你的問題)_,所以也許你想使用'Set-Location -Path {C:\ MyFolder} \ packages \ SpecRun.Runner。{version} \ tools'。 – Compo

+0

@Compo。我正在尋找一個SpecFlow.bat文件的目錄(我把它放在與SpecRun.exe相同的目錄中),僅基於文件名的模式匹配。我可以專門導航到'{C:\ MyFolder} \ packages \ SpecRun.Runner。{version} \ tools',但是如果「SpecRun.Runner。{version}」的版本會發生變化呢? – Konzy262

+1

Konzy262,從'Get-ChildItem'搜索中將'Set-Location'設置爲檢索路徑嗎?另外,'Start-Process'有一個'-WorkingDirectory'參數,它可以用來完成你所需要的,_(默認沒有它就是當前目錄)_。它也有一個'-ArgumentList'參數可以使用。 – Compo

回答

0

你可以這樣做:

$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