我有一個路徑串列和一個可執行檔案串列,我需要將它們連接在一起并在每個所述路徑下查找每個檔案。但是,當我運行我的代碼時,我看到可執行檔案沒有被連接,只是作為新行添加。此外,它僅被添加到串列中的最后一個物件。請參閱下面的代碼、輸出和所需輸出:
代碼:
$final_paths = @();
$paths = @("C:\Program Files\Microsoft Office\Office15", "C:\Program Files\Microsoft
Office Servers\OFFICE15");
$exes = @("MSOCF.DLL", "access.exe", "word.exe", "wordCnv.exe", "WordViewer.exe", "Excel.exe", "ExcelCnv.exe", "ExcelViewer.exe", "PowerPoint.exe",
"PowerPointViewer.exe", "PowerPointCnv.exe", "Publisher.exe", "Project.exe", "OneNote.exe", "InfoPath.exe Groove.exe", "FrontPage.exe",
"SharePointDesigner.exe", "Visio.exe", "VisioViewer.exe", "Lync.exeOutlook.exe", "WINPROJ.EXE")
foreach ($exe in $exes)
{
$final_paths = $paths"\$exe";
write-output $final_paths;
}
foreach ($found_path in $final_paths)
{
$file = Get-Item -Path $found_path -ErrorAction Ignore;
Write-Output $file
樣本輸出:
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Excel.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
ExcelCnv.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
ExcelViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPoint.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPointViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPointCnv.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Publisher.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Project.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
OneNote.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
InfoPath.exe Groove.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
FrontPage.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
SharePointDesigner.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Visio.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
VisioViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Lync.exeOutlook.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
WINPROJ
...
...
...
you get the idea
期望的輸出:
我只想要作為字串回傳的檔案名,如下所示:
WIINPROJ
uj5u.com熱心網友回復:
為什么要嘗試將路徑連接成全名,然后嘗試使用Get-Item該檔案是否存在?
使用Get-ChildItem事情會容易得多,因為
- 它可以處理引數中的檔案夾路徑陣列
-Path - 您只獲得通過
Where-Object子句過濾的特定檔案回傳的 FileInfo 物件,因此您無需在之后檢查它們的存在 - 除了檔案名之外,您還可以獲得更多資訊,因此您可以稍后在輸出中確定您想要的內容
$paths = 'C:\Program Files\Microsoft Office\Office15', 'C:\Program Files\Microsoft Office Servers\OFFICE15'
$exes = 'MSOCF.DLL', 'access.exe', 'word.exe', 'wordCnv.exe', 'WordViewer.exe', 'Excel.exe', 'ExcelCnv.exe', 'ExcelViewer.exe', 'PowerPoint.exe',
'PowerPointViewer.exe', 'PowerPointCnv.exe', 'Publisher.exe', 'Project.exe', 'OneNote.exe', 'InfoPath.exe Groove.exe', 'FrontPage.exe',
'SharePointDesigner.exe', 'Visio.exe', 'VisioViewer.exe', 'Lync.exeOutlook.exe', 'WINPROJ.EXE'
$files = Get-ChildItem -Path $paths -File | Where-Object { $exes -contains $_.Name }
現在您決定要從 $files 集合中檢索什么屬性
# just the Name?
$files.Name | Select-Object -Unique # unique in case you found the same exe in both folders
# the Fullname perhaps?
$files.FullName
uj5u.com熱心網友回復:
您可以使用以下Path.Combine方法System.IO:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore
}
}
$finalPaths.Name # => Should be the Names of the files found
或者,正如Lee_Dailey在他的評論中建議的那樣,您可以使用Join-Path來實作相同的結果:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408675.html
標籤:
