feat: Add recursive search for XLS files in subfolders

This commit is contained in:
Bobby Abellana (aider) 2025-02-21 09:39:39 -08:00
parent 004a3d239e
commit cc799c7d68

View File

@ -248,21 +248,14 @@ function List-XlsFiles {
throw "Folder not found: $($_.Exception.Message)" throw "Folder not found: $($_.Exception.Message)"
} }
$items = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $folderItem.id $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 { } else {
$script:txtStatus.Text += "Searching in root folder`n" $script:txtStatus.Text += "Searching in root folder`n"
$items = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $root.Id
}
foreach ($item in $items) { # Call the recursive function to process files and subfolders
$script:txtStatus.Text += "Checking file: $($item.Name)`n" $fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $root.Id
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"
}
} }
if ($fileList.Count -gt 0) { if ($fileList.Count -gt 0) {
@ -277,3 +270,37 @@ function List-XlsFiles {
$script:txtStatus.Text += "Error listing files: $($_.Exception.Message)`n" $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
}