Skip to content

Instantly share code, notes, and snippets.

@gabceb
Created May 3, 2011 22:36
Show Gist options
  • Select an option

  • Save gabceb/954418 to your computer and use it in GitHub Desktop.

Select an option

Save gabceb/954418 to your computer and use it in GitHub Desktop.
Powershell script to convert all xls documents to xlsx in a folder recursively
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
write-host $xlFixedFormat
$excel = New-Object -ComObject excel.application
$excel.visible = $true
$folderpath = "C:\Users\gabceb\Documents\testXLS"
$filetype ="*xls"
Get-ChildItem -Path $folderpath -Include $filetype -recurse |
ForEach-Object `
{
$path = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
"Converting $path"
$workbook = $excel.workbooks.open($_.fullname)
$path += ".xlsx"
$workbook.saveas($path, $xlFixedFormat)
$workbook.close()
$oldFolder = $path.substring(0, $path.lastIndexOf("\")) + "\old"
write-host $oldFolder
if(-not (test-path $oldFolder))
{
new-item $oldFolder -type directory
}
move-item $_.fullname $oldFolder
}
$excel.Quit()
$excel = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
@riskeez

riskeez commented Feb 8, 2021

Copy link
Copy Markdown

Thanks for the script!

I've done small modifications to make it work in 2021 (to solve aforementioned SaveAs exception issue).
https://gist.github.com/riskeez/096f3ee6bc23d35ed7730bbd36b33c44

@ace080

ace080 commented Mar 15, 2022

Copy link
Copy Markdown

Thanks for the script!

I've done small modifications to make it work in 2021 (to solve aforementioned SaveAs exception issue). https://gist.github.com/RichMcLaren/096f3ee6bc23d35ed7730bbd36b33c44

That link seems dead, do you have another link to help get the saveas working?

@riskeez

riskeez commented Mar 20, 2022

Copy link
Copy Markdown

That link seems dead, do you have another link to help get the saveas working?

@ace080 I've fixed the link in my original message now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment