【发布时间】: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