Need to convert the file extension from .xls to .xlsx using batch script

Hi All,

With the help of batch script i can rename the file extension but the original format remains same.Is there any command to open the
(.xls) files automatically from a folder and save it to (.xlsx) format without any manual intervention.

Please suggest.

Thanks,
Nithya
Nithya KovuruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Nothing built-in. You need to load the file into the application, then save as a different file type. This can be done with Office Automation, using VB Script or PowerShell.
Is it something you have to do a lot and all the time?
0
Hello ThereSystem AdministratorCommented:
I think this might help you: here.

Or just try this:
Sub Convertxlsxlsx()


Dim strCurrentFileExt As String
Dim strNewFileExt As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim xlFile As Workbook
Dim strNewName As String
Dim strFolderPath As String


strCurrentFileExt = ".xls"
strNewFileExt = ".xlsx"


strFolderPath = "C:\Users\USER\Downloads\CDS"
If Right(strFolderPath, 1) <> "" Then
strFolderPath = strFolderPath & ""
End If


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getfolder(strFolderPath)
For Each objFile In objFolder.Files
strNewName = objFile.Name
If Right(strNewName, Len(strCurrentFileExt)) = strCurrentFileExt Then
Set xlFile = Workbooks.Open(objFile.Path, , True)
strNewName = Replace(strNewName, strCurrentFileExt, strNewFileExt)
Application.DisplayAlerts = False
Select Case strNewFileExt
Case ".xlsx"
xlFile.SaveAs strFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbook
Case ".xlsm"
xlFile.SaveAs strFolderPath & strNewName, XlFileFormat.xlOpenXMLWorkbookMacroEnabled
End Select
xlFile.Close
Application.DisplayAlerts = True
End If
Next objFile


ClearMemory:
strCurrentFileExt = vbNullString
strNewFileExt = vbNullString
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set xlFile = Nothing
strNewName = vbNullString
strFolderPath = vbNullString


End Sub

Open in new window

0
Bill PrewCommented:
Here's a VBS I had provided as answer to an earlier question.  Save as a VBS file, and then run as follows from a command prompt, providing the path of the folder where the XLS file are you want to convert.

cscript yourfilename.vbs "c:\yourfolderpath"

Keep in mind that this saves as an XLSX file, not XLSM, so if any of the XLS files contain macros that could be a problem.

'Constants 
Const xlOpenXMLWorkbook = 51             '(without macro's in 2007-2016, xlsx)
Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
Const xlExcel12 = 50                     '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
Const xlExcel8 =56                       '(97-2003 format in Excel 2007-2016, xls)

' Extensions for old and new files
strOld = "xls"
strNew = "xlsx"

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get folder name to process off the command line, make sure it's valid
If (WScript.Arguments.Count > 0) Then
    strFolder = Wscript.Arguments(0)
    If Not objFSO.FolderExists(strFolder) Then
        WScript.StdErr.WriteLine "Specified folder does not exist."
        WScript.Quit
    End If
Else
    WScript.StdErr.WriteLine "No folder name specified to process."
    WScript.Quit
End If

' Access the folder to process
Set objFolder = objFSO.GetFolder(strFolder)

' Load Excel (hidden) for conversions
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False

' Process all files
For Each objFile In objFolder.Files
    ' Get full path to file
    strPath = objFile.Path
    ' Only convert desired files
    If LCase(objFSO.GetExtensionName(strPath)) = LCase(strOld) Then
        ' Display to console each file being converted
        Wscript.Echo "Converting """ & strPath & """"
        ' Load file into Excel and save as new format Excel file
        Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
        objWorkbook.SaveAs Replace(strPath, strOld, strNew), xlOpenXMLWorkbook
        objWorkbook.Close False
        Set objWorkbook = Nothing
    End If
Next

'Wrap up
objExcel.Quit
Set objExcel = Nothing
Set objFSO = Nothing

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Or you can just used libreoffice --headless, which allows conversion between any of the many formats LibreOffice understands.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I don't consider #a42408753 a suitable solution. Being VBA code, you have to implement that in any Office application, which is much over the top here. Using VBS as in #a42409005 is suitable in general, and I would agree to accept that, as it would work well enough to be useful.
0
Hello ThereSystem AdministratorCommented:
From my personal conversation with Community Support Moderator:

In inactive question if other answers are possible and helpful with potential and possible solution, they should be marked.
0
Bill PrewCommented:
Please provide your recommendations of how you feel this question should be closed. When doing so, be sure to include the CommentID of each comment, as well as a short explanation of why those comments should be considered a solution.
In this case I believe that #a42409005 is the right solution to be accepted.  It meets the authors need and can be easily run in a batch mode as requested by the author.  While #a42408753 is another approach, since it uses a VBA approach it requires manual intervention to execute, and would not match the request for a scripted batch approach as well, for this particular question.

At the end of the day it would be great if the author could revisit and guide the closure property, but without that a moderator will have to make this judgement call.


»bp
0
Hello ThereSystem AdministratorCommented:
#a42409005 suggestion is maybe more suitable but #a42408753 suggestion is a possible solution as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.