ConvertsXLStoXLSX and save to original location
This commit is contained in:
parent
e29391639f
commit
7822f074c4
@ -8,8 +8,3 @@ function Convert-Files_Click { # Rename to avoid conflict
|
|||||||
$script:txtStatus.Text += "Starting conversion...`n"
|
$script:txtStatus.Text += "Starting conversion...`n"
|
||||||
Convert-Files # Call the function in SharePointFunctions.ps1
|
Convert-Files # Call the function in SharePointFunctions.ps1
|
||||||
}
|
}
|
||||||
|
|
||||||
function Move-Files_Click { # Rename to avoid conflict
|
|
||||||
$script:txtStatus.Text += "Moving files...`n"
|
|
||||||
Move-Files # Call the function in SharePointFunctions.ps1
|
|
||||||
}
|
|
||||||
|
|||||||
15
MainForm.ps1
15
MainForm.ps1
@ -60,12 +60,6 @@ $btnConvert.Size = New-Object System.Drawing.Size(120, 30)
|
|||||||
$btnConvert.Text = "Convert to XLSX"
|
$btnConvert.Text = "Convert to XLSX"
|
||||||
$form.Controls.Add($btnConvert)
|
$form.Controls.Add($btnConvert)
|
||||||
|
|
||||||
$btnMove = New-Object System.Windows.Forms.Button
|
|
||||||
$btnMove.Location = New-Object System.Drawing.Point(300, 170)
|
|
||||||
$btnMove.Size = New-Object System.Drawing.Size(120, 30)
|
|
||||||
$btnMove.Text = "Move Files"
|
|
||||||
$form.Controls.Add($btnMove)
|
|
||||||
|
|
||||||
# Create Checkbox for Recursive Listing
|
# Create Checkbox for Recursive Listing
|
||||||
$chkRecursive = New-Object System.Windows.Forms.CheckBox
|
$chkRecursive = New-Object System.Windows.Forms.CheckBox
|
||||||
$chkRecursive.Location = New-Object System.Drawing.Point(20, 205)
|
$chkRecursive.Location = New-Object System.Drawing.Point(20, 205)
|
||||||
@ -96,10 +90,9 @@ $dataGridView.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeCo
|
|||||||
$form.Controls.Add($dataGridView)
|
$form.Controls.Add($dataGridView)
|
||||||
|
|
||||||
# Add columns to DataGridView
|
# Add columns to DataGridView
|
||||||
$dataGridView.ColumnCount = 3
|
$dataGridView.ColumnCount = 2
|
||||||
$dataGridView.Columns[0].Name = "Original Path"
|
$dataGridView.Columns[0].Name = "Original Path"
|
||||||
$dataGridView.Columns[1].Name = "Original File Name"
|
$dataGridView.Columns[1].Name = "Original File Name"
|
||||||
$dataGridView.Columns[2].Name = "Temp Path"
|
|
||||||
|
|
||||||
# Add Save Config button
|
# Add Save Config button
|
||||||
$btnSaveConfig = New-Object System.Windows.Forms.Button
|
$btnSaveConfig = New-Object System.Windows.Forms.Button
|
||||||
@ -108,8 +101,7 @@ $btnSaveConfig.Size = New-Object System.Drawing.Size(80, 20)
|
|||||||
$btnSaveConfig.Text = "Save Config"
|
$btnSaveConfig.Text = "Save Config"
|
||||||
$btnSaveConfig.Add_Click({
|
$btnSaveConfig.Add_Click({
|
||||||
Save-Config -SiteUrl $txtSiteUrl.Text `
|
Save-Config -SiteUrl $txtSiteUrl.Text `
|
||||||
-ProductionLibrary $txtProdLib.Text `
|
-ProductionLibrary $txtProdLib.Text
|
||||||
-TempLibrary $txtTempLib.Text
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# Add Connect button
|
# Add Connect button
|
||||||
@ -126,7 +118,6 @@ $btnConnect.Add_Click({
|
|||||||
$form.Controls.AddRange(@(
|
$form.Controls.AddRange(@(
|
||||||
$txtSiteUrl,
|
$txtSiteUrl,
|
||||||
$txtProdLib,
|
$txtProdLib,
|
||||||
$txtTempLib,
|
|
||||||
$txtFolder,
|
$txtFolder,
|
||||||
$btnBrowse,
|
$btnBrowse,
|
||||||
$btnSaveConfig,
|
$btnSaveConfig,
|
||||||
@ -141,13 +132,11 @@ if (-not (Load-Config)) {
|
|||||||
else {
|
else {
|
||||||
$txtSiteUrl.Text = $config.SiteUrl
|
$txtSiteUrl.Text = $config.SiteUrl
|
||||||
$txtProdLib.Text = $config.ProductionLibrary
|
$txtProdLib.Text = $config.ProductionLibrary
|
||||||
$txtTempLib.Text = $config.TempLibrary
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Attach event handlers
|
# Attach event handlers
|
||||||
$btnList.Add_Click({ List-XlsFiles_Click })
|
$btnList.Add_Click({ List-XlsFiles_Click })
|
||||||
$btnConvert.Add_Click({ Convert-Files_Click })
|
$btnConvert.Add_Click({ Convert-Files_Click })
|
||||||
$btnMove.Add_Click({ Move-Files_Click })
|
|
||||||
$btnBrowse.Add_Click({
|
$btnBrowse.Add_Click({
|
||||||
try {
|
try {
|
||||||
if (-not (Connect-SharePoint)) {
|
if (-not (Connect-SharePoint)) {
|
||||||
|
|||||||
@ -282,7 +282,7 @@ function List-XlsFiles {
|
|||||||
$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
|
||||||
foreach ($file in $fileList) {
|
foreach ($file in $fileList) {
|
||||||
$row = @($file.OriginalPath, $file.OriginalFileName, $file.TempPath)
|
$row = @($file.OriginalPath, $file.OriginalFileName)
|
||||||
$script:dataGridView.Rows.Add($row)
|
$script:dataGridView.Rows.Add($row)
|
||||||
}
|
}
|
||||||
#$fileList | ForEach-Object {
|
#$fileList | ForEach-Object {
|
||||||
@ -341,17 +341,6 @@ function Get-XlsFilesRecursive {
|
|||||||
$allFiles += [PSCustomObject]@{
|
$allFiles += [PSCustomObject]@{
|
||||||
OriginalPath = $item.WebUrl
|
OriginalPath = $item.WebUrl
|
||||||
OriginalFileName = $item.Name
|
OriginalFileName = $item.Name
|
||||||
TempPath = if ($script:txtFolder.Text) {
|
|
||||||
# Extract the relative path from the original path
|
|
||||||
$relativePath = $item.ParentReference.Path -replace ".*root:/", ""
|
|
||||||
if ($relativePath) {
|
|
||||||
"$($script:txtTempLib.Text)/$relativePath/$($item.Name)".Replace(".xls", ".xlsx")
|
|
||||||
} else {
|
|
||||||
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -396,17 +385,6 @@ function Get-XlsFilesCurrentFolder {
|
|||||||
$allFiles += [PSCustomObject]@{
|
$allFiles += [PSCustomObject]@{
|
||||||
OriginalPath = $item.WebUrl
|
OriginalPath = $item.WebUrl
|
||||||
OriginalFileName = $item.Name
|
OriginalFileName = $item.Name
|
||||||
TempPath = if ($script:txtFolder.Text) {
|
|
||||||
# Extract the relative path from the original path
|
|
||||||
$relativePath = $item.ParentReference.Path -replace ".*root:/", ""
|
|
||||||
if ($relativePath) {
|
|
||||||
"$($script:txtTempLib.Text)/$relativePath/$($item.Name)".Replace(".xls", ".xlsx")
|
|
||||||
} else {
|
|
||||||
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -416,11 +394,6 @@ function Get-XlsFilesCurrentFolder {
|
|||||||
|
|
||||||
function Convert-Files {
|
function Convert-Files {
|
||||||
try {
|
try {
|
||||||
# Ensure the Testing library exists before proceeding
|
|
||||||
if (-not (Ensure-TestingLibraryExists)) {
|
|
||||||
throw "Failed to ensure Testing library exists"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get site and drive information first
|
# Get site and drive information first
|
||||||
$script:txtStatus.Text += "Getting SharePoint site information...`n"
|
$script:txtStatus.Text += "Getting SharePoint site information...`n"
|
||||||
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
||||||
@ -668,27 +641,14 @@ function Convert-Files {
|
|||||||
throw "Converted file not found at: $xlsxPath"
|
throw "Converted file not found at: $xlsxPath"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the drive for the temp library
|
# Get the folder path from the original file
|
||||||
$tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
$folderPath = $script:txtFolder.Text
|
||||||
"Documents"
|
|
||||||
} else {
|
|
||||||
$script:txtTempLib.Text
|
|
||||||
}
|
|
||||||
$tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary }
|
|
||||||
if (-not $tempDrive) {
|
|
||||||
throw "Temp library not found: $tempLibrary"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Use the correct drive ID
|
|
||||||
$driveId = $tempDrive.Id
|
|
||||||
|
|
||||||
# Extract folder path from TempPath, but remove the library name prefix
|
|
||||||
$relativePath = $file.TempPath -replace "/[^/]+$", "" # Extract folder path from TempPath
|
|
||||||
$relativePath = $relativePath -replace "^$($script:txtTempLib.Text)/", "" # Remove library name prefix
|
|
||||||
$uploadFileName = $xlsxFileName # Use the converted file name
|
$uploadFileName = $xlsxFileName # Use the converted file name
|
||||||
|
|
||||||
# Construct the upload URI for the temp library, properly encoding the path
|
# Construct the upload URI for the original library
|
||||||
$uploadPath = if ($relativePath) {
|
$uploadPath = if ($folderPath) {
|
||||||
|
# Extract the relative path if needed
|
||||||
|
$relativePath = $folderPath -replace "/sites/tax/Shared Documents/", ""
|
||||||
"$relativePath/$uploadFileName" # Include folder structure
|
"$relativePath/$uploadFileName" # Include folder structure
|
||||||
} else {
|
} else {
|
||||||
$uploadFileName # Just use the filename to save in root
|
$uploadFileName # Just use the filename to save in root
|
||||||
@ -703,6 +663,7 @@ function Convert-Files {
|
|||||||
|
|
||||||
# Fix the URI construction to avoid the colon issue
|
# Fix the URI construction to avoid the colon issue
|
||||||
$baseUri = "https://graph.microsoft.com/v1.0/drives"
|
$baseUri = "https://graph.microsoft.com/v1.0/drives"
|
||||||
|
$driveId = $script:driveId
|
||||||
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
|
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
|
||||||
|
|
||||||
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
|
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
|
||||||
@ -713,9 +674,7 @@ function Convert-Files {
|
|||||||
# Upload the file
|
# Upload the file
|
||||||
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
||||||
|
|
||||||
if ($script:txtStatus) {
|
$script:txtStatus.Text += "Successfully uploaded to library: /sites/tax/$libraryName/$uploadPath`n"
|
||||||
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadPath`n"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||||
@ -856,151 +815,3 @@ function Move-Files {
|
|||||||
$script:txtStatus.Text += "Stack Trace: $($_.Exception.StackTrace)`n"
|
$script:txtStatus.Text += "Stack Trace: $($_.Exception.StackTrace)`n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to ensure the temp library exists
|
|
||||||
function Ensure-TestingLibraryExists {
|
|
||||||
try {
|
|
||||||
# Get the library name from TempLibrary field
|
|
||||||
$tempLibraryName = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
|
||||||
"Documents"
|
|
||||||
} else {
|
|
||||||
$script:txtTempLib.Text
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "Checking if $tempLibraryName library exists...`n"
|
|
||||||
} else {
|
|
||||||
Write-Host "Checking if $tempLibraryName library exists..."
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get site and drive information
|
|
||||||
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
|
||||||
|
|
||||||
if (-not $site) {
|
|
||||||
throw "Could not find SharePoint site"
|
|
||||||
}
|
|
||||||
|
|
||||||
$drives = Get-MgSiteDrive -SiteId $site.id
|
|
||||||
$tempLibrary = $drives | Where-Object { $_.Name -eq $tempLibraryName }
|
|
||||||
|
|
||||||
if (-not $tempLibrary) {
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "$tempLibraryName library not found. Creating library...`n"
|
|
||||||
} else {
|
|
||||||
Write-Host "$tempLibraryName library not found. Creating library..."
|
|
||||||
}
|
|
||||||
|
|
||||||
# Create the library
|
|
||||||
$createLibraryUri = "https://graph.microsoft.com/v1.0/sites/$($site.id)/lists"
|
|
||||||
$body = @{
|
|
||||||
displayName = $tempLibraryName
|
|
||||||
list = @{
|
|
||||||
template = "documentLibrary"
|
|
||||||
}
|
|
||||||
} | ConvertTo-Json
|
|
||||||
|
|
||||||
Invoke-MgGraphRequest -Uri $createLibraryUri -Method POST -Body $body -ContentType "application/json"
|
|
||||||
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "$tempLibraryName library created successfully.`n"
|
|
||||||
} else {
|
|
||||||
Write-Host "$tempLibraryName library created successfully."
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "$tempLibraryName library already exists.`n"
|
|
||||||
} else {
|
|
||||||
Write-Host "$tempLibraryName library already exists."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $true
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "Error ensuring library exists: $($_.Exception.Message)`n"
|
|
||||||
} else {
|
|
||||||
Write-Host "Error ensuring library exists: $($_.Exception.Message)"
|
|
||||||
}
|
|
||||||
return $false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Upload-ConvertedFile {
|
|
||||||
param (
|
|
||||||
[Parameter(Mandatory=$true)]
|
|
||||||
[string]$xlsxPath,
|
|
||||||
[Parameter(Mandatory=$true)]
|
|
||||||
[string]$uploadFolder,
|
|
||||||
[Parameter(Mandatory=$true)]
|
|
||||||
[string]$uploadFileName
|
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
|
||||||
# Verify file exists before trying to read it
|
|
||||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
|
||||||
throw "Converted file not found at: $xlsxPath"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get the destination folder from the TempLibrary field
|
|
||||||
$tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
|
||||||
"Documents"
|
|
||||||
} else {
|
|
||||||
$script:txtTempLib.Text
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get the drive for the temp library
|
|
||||||
$tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary }
|
|
||||||
if (-not $tempDrive) {
|
|
||||||
throw "Temp library not found: $tempLibrary"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Use the correct drive ID
|
|
||||||
$driveId = $tempDrive.Id
|
|
||||||
|
|
||||||
# Extract folder path from TempPath, but remove the library name prefix
|
|
||||||
$relativePath = $file.TempPath -replace "/[^/]+$", "" # Extract folder path from TempPath
|
|
||||||
$relativePath = $relativePath -replace "^$($script:txtTempLib.Text)/", "" # Remove library name prefix
|
|
||||||
$uploadFileName = $xlsxFileName # Use the converted file name
|
|
||||||
|
|
||||||
# Construct the upload URI for the temp library, properly encoding the path
|
|
||||||
$uploadPath = if ($relativePath) {
|
|
||||||
"$relativePath/$uploadFileName" # Include folder structure
|
|
||||||
} else {
|
|
||||||
$uploadFileName # Just use the filename to save in root
|
|
||||||
}
|
|
||||||
|
|
||||||
# URL encode the path but preserve forward slashes and encode spaces as %20
|
|
||||||
$encodedPath = $uploadPath.Split('/') |
|
|
||||||
ForEach-Object {
|
|
||||||
[System.Web.HttpUtility]::UrlEncode($_).Replace("+", "%20")
|
|
||||||
} |
|
|
||||||
Join-String -Separator '/'
|
|
||||||
|
|
||||||
# Fix the URI construction to avoid the colon issue
|
|
||||||
$baseUri = "https://graph.microsoft.com/v1.0/drives"
|
|
||||||
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
|
|
||||||
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Read the file content
|
|
||||||
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
|
||||||
|
|
||||||
# Upload the file
|
|
||||||
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
|
||||||
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadPath`n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
if ($script:txtStatus) {
|
|
||||||
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
|
||||||
} else {
|
|
||||||
Write-Host "Error uploading file: $($_.Exception.Message)"
|
|
||||||
}
|
|
||||||
throw
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user