feat: Add recursive listing checkbox to MainForm

This commit is contained in:
Bobby Abellana (aider) 2025-02-21 10:13:39 -08:00
parent 2c18235425
commit f1e3a09cc0
2 changed files with 85 additions and 10 deletions

View File

@ -65,6 +65,14 @@ $btnMove.Size = New-Object System.Drawing.Size(120, 30)
$btnMove.Text = "Move Files"
$form.Controls.Add($btnMove)
# Create Checkbox for Recursive Listing
$chkRecursive = New-Object System.Windows.Forms.CheckBox
$chkRecursive.Location = New-Object System.Drawing.Point(20, 205)
$chkRecursive.Size = New-Object System.Drawing.Size(150, 20)
$chkRecursive.Text = "List Recursively"
$chkRecursive.Checked = $true # Default to recursive
$form.Controls.Add($chkRecursive)
# Create Status TextBox (Top Right)
$txtStatus = New-Object System.Windows.Forms.TextBox
$txtStatus.Location = New-Object System.Drawing.Point(450, 20) # Top Right Corner
@ -77,8 +85,8 @@ $form.Controls.Add($txtStatus)
# Create DataGridView (Lower 2/3)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Location = New-Object System.Drawing.Point(20, 210)
$dataGridView.Size = New-Object System.Drawing.Size(760, 340) # Lower 2/3 of the form
$dataGridView.Location = New-Object System.Drawing.Point(20, 230)
$dataGridView.Size = New-Object System.Drawing.Size(760, 320) # Lower 2/3 of the form
$dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
[System.Windows.Forms.AnchorStyles]::Left -bor `
[System.Windows.Forms.AnchorStyles]::Right -bor `
@ -121,7 +129,8 @@ $form.Controls.AddRange(@(
$txtFolder,
$btnBrowse,
$btnSaveConfig,
$btnConnect
$btnConnect,
$chkRecursive # Add the checkbox to the form
))
# Load configuration
@ -159,6 +168,7 @@ $btnBrowse.Add_Click({
# Store the DataGridView and txtStatus in script scope
$script:dataGridView = $dataGridView
$script:txtStatus = $txtStatus
$script:chkRecursive = $chkRecursive
# Show form
$form.Add_Shown({ $form.Activate() })

View File

@ -6,9 +6,11 @@ function Load-Config {
$script:config = Get-Content $configPath -Raw | ConvertFrom-Json
return $true
} else {
$script:txtStatus.Text += "Error loading configuration: $($_.Exception.Message)`n"
return $false
}
} catch {
}
catch {
$script:txtStatus.Text += "Error loading configuration: $($_.Exception.Message)`n"
return $false
}
@ -200,6 +202,9 @@ function Connect-SharePoint {
function List-XlsFiles {
try {
# Clear the DataGridView before listing new files
$script:dataGridView.Rows.Clear()
$script:txtStatus.Text += "Connecting to SharePoint...`n"
if (-not (Connect-SharePoint)) {
throw "Failed to connect to SharePoint"
@ -247,23 +252,38 @@ function List-XlsFiles {
} catch {
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
if ($script:chkRecursive.Checked) {
$script:txtStatus.Text += "Listing files recursively...`n"
$fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $folderItem.id
} else {
$script:txtStatus.Text += "Listing files in current folder only...`n"
$fileList = Get-XlsFilesCurrentFolder -DriveId $drive.Id -DriveItemId $folderItem.id
}
} else {
$script:txtStatus.Text += "Searching in root folder`n"
# Call the recursive function to process files and subfolders
$fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $root.Id
if ($script:chkRecursive.Checked) {
$script:txtStatus.Text += "Listing files recursively...`n"
$fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $root.Id
} else {
$script:txtStatus.Text += "Listing files in current folder only...`n"
$fileList = Get-XlsFilesCurrentFolder -DriveId $drive.Id -DriveItemId $root.Id
}
}
if ($fileList.Count -gt 0) {
$script:txtStatus.Text += "Saving file list...`n"
# Display each file on a new line
$fileList | ForEach-Object {
$script:txtStatus.Text += "$($_.OriginalFileName)`n"
foreach ($file in $fileList) {
$row = @($file.OriginalPath, $file.OriginalFileName, $file.TempPath)
$script:dataGridView.Rows.Add($row)
}
#$fileList | ForEach-Object {
# $script:txtStatus.Text += "$($_.OriginalFileName)`n"
#}
$fileList | ConvertTo-Json | Out-File "$env:TEMP\FileList.json"
$script:txtStatus.Text += "Found $($fileList.Count) XLS files`n"
} else {
@ -324,3 +344,48 @@ function Get-XlsFilesRecursive {
return $allFiles
}
# Function to get XLS files from only the current folder
function Get-XlsFilesCurrentFolder {
param (
[string]$DriveId,
[string]$DriveItemId
)
$allFiles = @()
# Check if DriveId or DriveItemId is null or empty
if ([string]::IsNullOrEmpty($DriveId)) {
$script:txtStatus.Text += "Error: DriveId is null or empty`n"
return $allFiles
}
if ([string]::IsNullOrEmpty($DriveItemId)) {
$script:txtStatus.Text += "Error: DriveItemId is null or empty`n"
return $allFiles
}
# Get items in the current folder
try {
$items = Get-MgDriveItemChild -DriveId $DriveId -DriveItemId $DriveItemId -ErrorAction Stop
}
catch {
$script:txtStatus.Text += "Error getting items: $($_.Exception.Message)`n"
return $allFiles
}
foreach ($item in $items) {
$script:txtStatus.Text += "Checking item: $($item.Name)`n"
# If it's an Excel file, add it to the list
if ($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
}