首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将列表连接到路径的末尾- Powershell

将列表连接到路径的末尾- Powershell
EN

Stack Overflow用户
提问于 2022-01-10 22:51:24
回答 2查看 86关注 0票数 0

我有一个路径列表和一个可执行文件列表,我需要将它们连接在一起,并在每个所述路径下查找每个文件。然而,当我运行我的代码时,我看到可执行文件并没有被连接,只是作为一个新的行添加。此外,它只被添加到列表中的最后一个对象中。请参见下面的代码、输出和期望输出:

代码:

代码语言:javascript
运行
复制
   $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 

样本输出:

代码语言:javascript
运行
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-11 12:37:42

为什么要尝试将路径连接到全名,然后尝试使用Get-Item来确定该文件是否存在?

使用得到-儿童要容易得多,因为

  1. 它可以处理参数中文件夹路径的数组。
  2. 您只获得通过一个FileInfo子句过滤的特定文件返回的Where-Object对象,因此以后不需要检查它们是否存在。
  3. 您可以获得更多的信息,而不仅仅是文件名,因此您可以在稍后决定输出中所需要的内容。
代码语言:javascript
运行
复制
$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集合中检索哪个属性。

代码语言:javascript
运行
复制
# just the Name?
$files.Name | Select-Object -Unique  # unique in case you found the same exe in both folders

# the Fullname perhaps?
$files.FullName
票数 1
EN

Stack Overflow用户

发布于 2022-01-10 22:58:23

您可以使用来自Path.CombineSystem.IO方法进行如下操作:

代码语言:javascript
运行
复制
$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

或者,正如戴利在他的评论中所建议的那样,您可以使用Join-Path实现相同的结果:

代码语言:javascript
运行
复制
$finalPaths = foreach($path in $paths)
{
    foreach($exe in $exes)
    {
        Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70659767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档