From 1b695478e7baad39a5f21a8964e9a13f99d69ebf Mon Sep 17 00:00:00 2001 From: Mikkel Nordberg Date: Fri, 23 Jun 2017 15:55:06 +0200 Subject: [PATCH 1/5] Create Convert-XLSToXLSX --- Convert-XLSToXLSX | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Convert-XLSToXLSX diff --git a/Convert-XLSToXLSX b/Convert-XLSToXLSX new file mode 100644 index 0000000..5cfea92 --- /dev/null +++ b/Convert-XLSToXLSX @@ -0,0 +1,48 @@ +Function Convert-XLSToXLSX { +[CmdletBinding()] +PARAM +( + [parameter(Mandatory=$true, ValueFromPipeline)] + [ValidateScript({ + if(-Not ($_ | Test-Path) ){ + throw "File not found" + } + if(-Not ($_ | Test-Path -PathType Leaf) ){ + throw "Folder paths are not allowed" + } + return $true + })] + [string]$Path, + [parameter(Mandatory=$false)] + [switch]$Force +) + $xlFixedFormat = 51 #Constant for XLSX Workbook + $xlsFile = Get-Item -Path $Path + $xlsxPath = "{0}x" -f $xlsFile.FullName + + if($xlsFile.Extension.ToLower() -ne ".xls"){ + throw "Expected .xls extension" + } + + if(Test-Path -Path $xlsxPath){ + if($Force){ + Remove-Item $xlsxPath -Force + Write-Verbose $("Removed {0}" -f $xlsxPath) + } else { + throw "{0} already exists!" -f $xlsxPath + } + } + + try{ + $Excel = New-Object -ComObject "Excel.Application" + } catch { + throw "Could not create Excel.Application ComObject. Please verify that Excel is installed." + } + + $Excel.Visible = $false + $Excel.Workbooks.Open($xlsFile.FullName) | Out-Null + $Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat) + $Excel.ActiveWorkbook.Close() + $Excel.Quit() +} + From 8344118c1150b26158a86239e395040ff039d738 Mon Sep 17 00:00:00 2001 From: Mikkel Nordberg Date: Fri, 23 Jun 2017 15:56:10 +0200 Subject: [PATCH 2/5] Rename Convert-XLSToXLSX to Convert-XLSToXLSX.ps1 --- Convert-XLSToXLSX => Convert-XLSToXLSX.ps1 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Convert-XLSToXLSX => Convert-XLSToXLSX.ps1 (100%) diff --git a/Convert-XLSToXLSX b/Convert-XLSToXLSX.ps1 similarity index 100% rename from Convert-XLSToXLSX rename to Convert-XLSToXLSX.ps1 From ea0a5a7c7617f8a65654daad599053389595dbfd Mon Sep 17 00:00:00 2001 From: Mikkel Nordberg Date: Tue, 27 Jun 2017 13:20:38 +0200 Subject: [PATCH 3/5] Update and rename Convert-XLSToXLSX.ps1 to ConvertTo-ExcelXlsx.ps1 --- Convert-XLSToXLSX.ps1 => ConvertTo-ExcelXlsx.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Convert-XLSToXLSX.ps1 => ConvertTo-ExcelXlsx.ps1 (97%) diff --git a/Convert-XLSToXLSX.ps1 b/ConvertTo-ExcelXlsx.ps1 similarity index 97% rename from Convert-XLSToXLSX.ps1 rename to ConvertTo-ExcelXlsx.ps1 index 5cfea92..431924f 100644 --- a/Convert-XLSToXLSX.ps1 +++ b/ConvertTo-ExcelXlsx.ps1 @@ -1,4 +1,4 @@ -Function Convert-XLSToXLSX { +Function ConvertTo-ExcelXlsx { [CmdletBinding()] PARAM ( From c359560fd83a219894e71bea609eccb1f138a758 Mon Sep 17 00:00:00 2001 From: Mikkel Nordberg Date: Thu, 29 Jun 2017 12:23:24 +0200 Subject: [PATCH 4/5] Removed case-sesitivity check. -ne comparison operator is case insensitive so there is no need for .ToLower() --- ConvertTo-ExcelXlsx.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConvertTo-ExcelXlsx.ps1 b/ConvertTo-ExcelXlsx.ps1 index 431924f..16cebb8 100644 --- a/ConvertTo-ExcelXlsx.ps1 +++ b/ConvertTo-ExcelXlsx.ps1 @@ -20,7 +20,7 @@ PARAM $xlsFile = Get-Item -Path $Path $xlsxPath = "{0}x" -f $xlsFile.FullName - if($xlsFile.Extension.ToLower() -ne ".xls"){ + if($xlsFile.Extension -ne ".xls"){ throw "Expected .xls extension" } From 097f11a66110641678dcc3ee68f87bebaecdda65 Mon Sep 17 00:00:00 2001 From: Mikkel Nordberg Date: Thu, 29 Jun 2017 14:18:29 +0200 Subject: [PATCH 5/5] Minor patch Consolidated parameter validation. Added error handling on -Force if destination file cannot be deleted. --- ConvertTo-ExcelXlsx.ps1 | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ConvertTo-ExcelXlsx.ps1 b/ConvertTo-ExcelXlsx.ps1 index 16cebb8..64d6c32 100644 --- a/ConvertTo-ExcelXlsx.ps1 +++ b/ConvertTo-ExcelXlsx.ps1 @@ -3,19 +3,17 @@ Function ConvertTo-ExcelXlsx { PARAM ( [parameter(Mandatory=$true, ValueFromPipeline)] - [ValidateScript({ - if(-Not ($_ | Test-Path) ){ - throw "File not found" - } - if(-Not ($_ | Test-Path -PathType Leaf) ){ - throw "Folder paths are not allowed" - } - return $true - })] [string]$Path, [parameter(Mandatory=$false)] [switch]$Force ) + if(-Not ($Path | Test-Path) ){ + throw "File not found" + } + if(-Not ($Path | Test-Path -PathType Leaf) ){ + throw "Folder paths are not allowed" + } + $xlFixedFormat = 51 #Constant for XLSX Workbook $xlsFile = Get-Item -Path $Path $xlsxPath = "{0}x" -f $xlsFile.FullName @@ -26,7 +24,11 @@ PARAM if(Test-Path -Path $xlsxPath){ if($Force){ - Remove-Item $xlsxPath -Force + try { + Remove-Item $xlsxPath -Force + } catch { + throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath + } Write-Verbose $("Removed {0}" -f $xlsxPath) } else { throw "{0} already exists!" -f $xlsxPath