【问题标题】:Concatenate a list to the end of paths - Powershell将列表连接到路径的末尾 - Powershell
【发布时间】:2022-01-10 22:51:24
【问题描述】:

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

代码:

   $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

【问题讨论】:

  • 您正在将一个字符串添加到一个字符串数组中。这将为您提供您所得到的...一个在末尾添加了新字符串的数组。 [grin] ///// 你需要遍历 BOTH 数组...并且 otta 使用 Join-Path 而不是字符串连接。
  • 顺便说一句,如果您想要输出的只是 exe 的文件 name(没有该路径),那么在两个不同的路径中测试 .exe 有什么用?

标签: windows list powershell iteration concatenation


【解决方案1】:

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

使用Get-ChildItem 会容易很多,因为

  1. 它可以处理-Path参数中的数组文件夹路径
  2. 您只获得通过 Where-Object 子句过滤的特定文件返回的 FileInfo 对象,因此您无需在之后检查它们的存在
  3. 除了文件名之外,您还可以获得更多信息,因此您可以稍后在输出中确定您想要的内容
$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

【讨论】:

  • 返回:Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
  • @cynicalswan77 哎哟..您使用的是古老的 PowerShell 版本吗?如果是,请尝试至少升级到 3.0 版。如果这不可能,您需要在 Where-Object 子句{ !$_.PSIsContainer -and $exes -contains $_.Name } 中添加一个额外的测试。然后移除-File 开关。
  • 抱歉,误报。路径是 dir 对象,而不是字符串。转换后,效果很好。
【解决方案2】:

您可以为此使用System.IO 中的Path.Combine 方法:

$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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2015-03-31
    • 2016-04-13
    • 2013-06-22
    • 2018-06-22
    • 1970-01-01
    相关资源
    最近更新 更多