feat: Add recursive listing checkbox to MainForm
This commit is contained in:
parent
2c18235425
commit
f1e3a09cc0
16
MainForm.ps1
16
MainForm.ps1
@ -65,6 +65,14 @@ $btnMove.Size = New-Object System.Drawing.Size(120, 30)
|
|||||||
$btnMove.Text = "Move Files"
|
$btnMove.Text = "Move Files"
|
||||||
$form.Controls.Add($btnMove)
|
$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)
|
# Create Status TextBox (Top Right)
|
||||||
$txtStatus = New-Object System.Windows.Forms.TextBox
|
$txtStatus = New-Object System.Windows.Forms.TextBox
|
||||||
$txtStatus.Location = New-Object System.Drawing.Point(450, 20) # Top Right Corner
|
$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)
|
# Create DataGridView (Lower 2/3)
|
||||||
$dataGridView = New-Object System.Windows.Forms.DataGridView
|
$dataGridView = New-Object System.Windows.Forms.DataGridView
|
||||||
$dataGridView.Location = New-Object System.Drawing.Point(20, 210)
|
$dataGridView.Location = New-Object System.Drawing.Point(20, 230)
|
||||||
$dataGridView.Size = New-Object System.Drawing.Size(760, 340) # Lower 2/3 of the form
|
$dataGridView.Size = New-Object System.Drawing.Size(760, 320) # Lower 2/3 of the form
|
||||||
$dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
|
$dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
|
||||||
[System.Windows.Forms.AnchorStyles]::Left -bor `
|
[System.Windows.Forms.AnchorStyles]::Left -bor `
|
||||||
[System.Windows.Forms.AnchorStyles]::Right -bor `
|
[System.Windows.Forms.AnchorStyles]::Right -bor `
|
||||||
@ -121,7 +129,8 @@ $form.Controls.AddRange(@(
|
|||||||
$txtFolder,
|
$txtFolder,
|
||||||
$btnBrowse,
|
$btnBrowse,
|
||||||
$btnSaveConfig,
|
$btnSaveConfig,
|
||||||
$btnConnect
|
$btnConnect,
|
||||||
|
$chkRecursive # Add the checkbox to the form
|
||||||
))
|
))
|
||||||
|
|
||||||
# Load configuration
|
# Load configuration
|
||||||
@ -159,6 +168,7 @@ $btnBrowse.Add_Click({
|
|||||||
# Store the DataGridView and txtStatus in script scope
|
# Store the DataGridView and txtStatus in script scope
|
||||||
$script:dataGridView = $dataGridView
|
$script:dataGridView = $dataGridView
|
||||||
$script:txtStatus = $txtStatus
|
$script:txtStatus = $txtStatus
|
||||||
|
$script:chkRecursive = $chkRecursive
|
||||||
|
|
||||||
# Show form
|
# Show form
|
||||||
$form.Add_Shown({ $form.Activate() })
|
$form.Add_Shown({ $form.Activate() })
|
||||||
|
|||||||
@ -6,9 +6,11 @@ function Load-Config {
|
|||||||
$script:config = Get-Content $configPath -Raw | ConvertFrom-Json
|
$script:config = Get-Content $configPath -Raw | ConvertFrom-Json
|
||||||
return $true
|
return $true
|
||||||
} else {
|
} else {
|
||||||
|
$script:txtStatus.Text += "Error loading configuration: $($_.Exception.Message)`n"
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
} catch {
|
}
|
||||||
|
catch {
|
||||||
$script:txtStatus.Text += "Error loading configuration: $($_.Exception.Message)`n"
|
$script:txtStatus.Text += "Error loading configuration: $($_.Exception.Message)`n"
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
@ -200,6 +202,9 @@ function Connect-SharePoint {
|
|||||||
|
|
||||||
function List-XlsFiles {
|
function List-XlsFiles {
|
||||||
try {
|
try {
|
||||||
|
# Clear the DataGridView before listing new files
|
||||||
|
$script:dataGridView.Rows.Clear()
|
||||||
|
|
||||||
$script:txtStatus.Text += "Connecting to SharePoint...`n"
|
$script:txtStatus.Text += "Connecting to SharePoint...`n"
|
||||||
if (-not (Connect-SharePoint)) {
|
if (-not (Connect-SharePoint)) {
|
||||||
throw "Failed to connect to SharePoint"
|
throw "Failed to connect to SharePoint"
|
||||||
@ -247,23 +252,38 @@ function List-XlsFiles {
|
|||||||
} catch {
|
} catch {
|
||||||
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
|
# 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 {
|
} else {
|
||||||
$script:txtStatus.Text += "Searching in root folder`n"
|
$script:txtStatus.Text += "Searching in root folder`n"
|
||||||
|
|
||||||
# Call the recursive function to process files and subfolders
|
if ($script:chkRecursive.Checked) {
|
||||||
$fileList = Get-XlsFilesRecursive -DriveId $drive.Id -DriveItemId $root.Id
|
$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) {
|
if ($fileList.Count -gt 0) {
|
||||||
$script:txtStatus.Text += "Saving file list...`n"
|
$script:txtStatus.Text += "Saving file list...`n"
|
||||||
# Display each file on a new line
|
# Display each file on a new line
|
||||||
$fileList | ForEach-Object {
|
foreach ($file in $fileList) {
|
||||||
$script:txtStatus.Text += "$($_.OriginalFileName)`n"
|
$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"
|
$fileList | ConvertTo-Json | Out-File "$env:TEMP\FileList.json"
|
||||||
$script:txtStatus.Text += "Found $($fileList.Count) XLS files`n"
|
$script:txtStatus.Text += "Found $($fileList.Count) XLS files`n"
|
||||||
} else {
|
} else {
|
||||||
@ -324,3 +344,48 @@ function Get-XlsFilesRecursive {
|
|||||||
|
|
||||||
return $allFiles
|
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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user