将列表连接到路径的末尾 - Powershell
Concatenate a list to the end of paths - Powershell
我有一个路径列表和一个可执行文件列表,我需要将它们连接在一起并在每个所述路径下查找每个文件。但是,当我 运行 我的代码时,我看到可执行文件没有被连接,只是作为一个新行添加。此外,它仅被添加到列表中的最后一个对象。请参阅下面的代码、输出和所需的输出:
代码:
$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
为此,您可以使用 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 suggested in his comment, you can use Join-Path
实现相同的结果:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
}
}
为什么要尝试将路径连接成全名,然后尝试使用 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
我有一个路径列表和一个可执行文件列表,我需要将它们连接在一起并在每个所述路径下查找每个文件。但是,当我 运行 我的代码时,我看到可执行文件没有被连接,只是作为一个新行添加。此外,它仅被添加到列表中的最后一个对象。请参阅下面的代码、输出和所需的输出:
代码:
$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
为此,您可以使用 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 suggested in his comment, you can use Join-Path
实现相同的结果:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
}
}
为什么要尝试将路径连接成全名,然后尝试使用 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