How to call/fetch all files within a folder in powershell script

I am writing a powershell script for my cicd devops process in azure.
my need is :- whenever a file gets changed by a developer ( which is within a folder ) , then the whole folder with all files should get copied and sit inside a temp folder.
by which i can do the build for that folder.
I am talking this scenario of salesforce aura and lwc components.
my code till now looks like below , can someone helps me to get away with this issue.

echo "Entering Powershell"

echo $env:WORKSPACE

echo "Cleaning temp directory"

#Remove-Item -recurse temp -Force | Out-Null

$files = git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT HEAD~1 HEAD

echo $files  #Gives the name of file along with its path ,that has been changed, the lastest commit

echo "Re-creating temp directory"

new-item -Name "temp" -Force -ItemType directory | Out-Null

foreach($file in $files)

{

    echo "printing file name"

    #Replace slash

    

    $file = $file -replace "/","\"

   

    #Copy files to temp directory

    

    echo FA|xcopy $file temp\$file

    # echo $temp

    #Check extension

    $extn = [IO.Path]::GetExtension($file)

    if ($extn -eq ".cls" -or  $extn -eq ".page"  -or $extn -eq ".trigger" -or $extn -eq ".cmp" -or  $extn -eq ".email" -or  $extn -eq ".resource")

    {

        echo "inside meta"

        $metafile = $file + "-meta.xml"

        echo FA|xcopy $metafile temp\$metafile

        echo $temp

    }

    

}

Hi, welcome to the forum :wave:

I’m not 100% sure I’ve understood your requirement, and I’m also not clear exactly what you want to copy, for example if the path of the changed file is folder1\folder2\changedfile would you want to copy folder2 to temp or folder1 and all subfolders and files?

I have made an assumption that it’s the former but you can modify the technique for the latter if required.

I have also assumed that your git command gives the full path, e.g. C:\folder1\folder2\changedfile.txt - I can’t check that on this machine.

foreach ($file in $files) {

    $folderPath = $file.FullName | Split-Path
    $folderName = $folderPath | Split-Path -Leaf
    
    # Check if the folder has already been copied
    # If multiple files have changed, we only need to copy the folder once

    if (-not(Test-Path E:\Temp\$folderName)) {
    
        Copy-Item -Path $folderPath -Destination E:\Temp\$folderName -Container -Recurse
    
    }

}