Home > Enterprise >  Cannot loop through objects in the folders
Cannot loop through objects in the folders

Time:06-29

I have two folders one folder has .pptx files and the other has .png files. My objective is to pick up file "A.pptx" from the folder having .pptx files and the corresponding "A.png" file from the folder having .png files and send them across in a mail body. I am unable to do so as i cannot nest the loops. The output I am having is A.pptx & C.png; B.pptx and C.png; C.pptx & C.png. My desired output is A.pptx & A.png; B.pptx & B.png; C.pptx & C.png. Quite new to PowerShell. Please help.

Get-ChildItem "C:\APPS\Deployments" -Filter *.pptx | 
Foreach-Object {
 $File = $_.FullName
 $DeploymentName =[System.IO.Path]::GetFileNameWithoutExtension($File)
  
 Get-ChildItem -Path "C:\APPS\Screenshots" -Filter *.png|
    Foreach-Object {
 $Screenshot = $_.FullName
 }
 
 try
{
 Send-MailMessage @smtpsettings  -Attachments $File,$Screenshot -Subject "FOR REVIEW & APPROVAL: GC.019.1: Separation of environments $DeploymentName - $year" -Priority High -Body  " <font face='Californian FB'> Hello ABC,  <br /> <br /> This email is in regards to executing Annual control  Separation of environments (Development, Acceptance and Production) for the $year for the deployment $DeploymentName<br /> <br /> Please find attached the landscape document and the screenshots as captured from the actual deployments.<br />  <br /> Evidences for review:<br />  <br />
1.  Latest landscape document<br />  <br />
2.  Screenshots from different environments as specified in the landscape document<br />  <br />
 <font face='Times New Roman' color='blue'> =====================================
 <br /> <br /> Thanks & Regards, <br /> Siddhartha Das<br /> -BodyAsHtml -Encoding utf32 -verbose -ErrorAction Stop
}

catch
{
    Write-Warning $_.Exception.Message
}
}

CodePudding user response:

I think this:

Get-ChildItem -Path "C:\APPS\Screenshots" -Filter *.png|
    Foreach-Object {
         $Screenshot = $_.FullName
     }

should be:

 Get-ChildItem -Path "C:\APPS\Screenshots" -Filter $DeploymentName.png

CodePudding user response:

$ppt = Get-ChildItem -Path "C:\Users\Desktop\ppt" -Filter *.pptx

$png=Get-ChildItem -Path "C:\Users\Desktop\png" -Filter *.png


foreach($item in $ppt.name){

$txtname=$item.Split('.')[0]

    foreach($item1 in $png.name){

    $pngname=$item1.Split('.')[0]
        if($pngname -match $txtname){
        $item,$item1
        
        }
    }

}

  • Related