diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index b8bd33e..98acc77 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -248,21 +248,14 @@ function List-XlsFiles { throw "Folder not found: $($_.Exception.Message)" } $items = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $folderItem.id + + # Call the recursive function to process files and subfolders + $fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $folderItem.id } else { $script:txtStatus.Text += "Searching in root folder`n" - $items = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $root.Id - } - - foreach ($item in $items) { - $script:txtStatus.Text += "Checking file: $($item.Name)`n" - if ($item.Name -like "*.xls" -and $item.Name -notlike "*.xlsx") { - $fileList += [PSCustomObject]@{ - OriginalPath = $item.WebUrl - OriginalFileName = $item.Name - TempPath = "$($script:txtTempLib.Text)/$($item.Name)" - } - $script:txtStatus.Text += "Found XLS file: $($item.Name)`n" - } + + # Call the recursive function to process files and subfolders + $fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $root.Id } if ($fileList.Count -gt 0) { @@ -277,3 +270,37 @@ function List-XlsFiles { $script:txtStatus.Text += "Error listing files: $($_.Exception.Message)`n" } } + +# Recursive function to get XLS files from a folder and its subfolders +function Get-XlsFilesRecursive { + param ( + [string]$DriveId, + [string]$DriveItemId + ) + + $allFiles = @() + + # Get items in the current folder + $items = Get-MgDriveItemChild -DriveId $DriveId -DriveItemId $DriveItemId + + foreach ($item in $items) { + $script:txtStatus.Text += "Checking item: $($item.Name)`n" + + # If it's a folder, recurse into it + if ($item.Folder) { + $script:txtStatus.Text += "Found folder: $($item.Name), recursing...`n" + $allFiles += Get-XlsFilesRecursive -DriveId $DriveId -DriveItemId $item.Id + } + # If it's an Excel file, add it to the list + elseif ($item.Name -like "*.xls" -or $item.Name -like "*.xlsx" -or $item.Name -like "*.xlsm") { + $script:txtStatus.Text += "Found XLS file: $($item.Name)`n" + $allFiles += [PSCustomObject]@{ + OriginalPath = $item.WebUrl + OriginalFileName = $item.Name + TempPath = "$($script:txtTempLib.Text)/$($item.Name)" + } + } + } + + return $allFiles +}