Home > Net >  Concatenate a list to the end of paths - Powershell
Concatenate a list to the end of paths - Powershell

Time:01-12

I have a list of paths and a list of executables files that I need to concatenate together and look for each file under each said path. However, when I run my code, I am seeing that the executables are not being concatenated, just added as a new line. Additionally, it is only being added to the last object in the list. See code, output, and desired output below:

Code:

   $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 

Sample Output:

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

Desired Output:

I simply want nothing but the file name returned as a string, like so:

WIINPROJ

CodePudding user response:

You can use the Path.Combine method from System.IO for this:

$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

Alternatively, as Lee_Dailey suggested in his comment, you can use Join-Path to achieve the same result:

$finalPaths = foreach($path in $paths)
{
    foreach($exe in $exes)
    {
        Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
    }
}

CodePudding user response:

Why try and concatenate paths into full names and then try to figure out using Get-Item if that file exists or not?

Using Get-ChildItem things would be a lot easier, because

  1. it can handle an array of folder paths in the -Path parameter
  2. you get only FileInfo objects returned for the specific files you filter through a Where-Object clause so you don't need to check their existance afterwards
  3. you get a lot more information than just the file name, so you can decide later what it is exactly you want in the output
$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 }

Now you decide what property you want to retrieve from the $files collection

# just the Name?
$files.Name | Select-Object -Unique  # unique in case you found the same exe in both folders

# the Fullname perhaps?
$files.FullName
  •  Tags:  
  • Related