用 chatgpt 写了一份,简单 review 了一下似乎是没有大问题
关键的那一行 objWorkbook.SaveAs strNewFile, 51 我不太确定 51 是否对应 xlsx 格式
还有就是把 objExcel 提到循环外面来运行效率会更高一些,不然每一次循环都会打开 excel 关闭 excel
还还有就是生成新文件名变量 strNewFile 时,没有利用到 objFSO 简化代码,而是用了 Left&Len 搁那儿截来截去,可读性不是很好
Please write a vbs script that can batch covert xls to xlsx.
```
Option Explicit
Dim objFSO, objFolder, objFiles, objExcel, objWorkbook
Dim strFolderPath, strExtension, strNewExtension, strFile, strNewFile
' set the folder path containing the XLS files
strFolderPath = "C:\Users\Username\Desktop\XLS_Files"
' set the file extension to look for
strExtension = ".xls"
' set the new file extension
strNewExtension = ".xlsx"
' create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' get the folder object
Set objFolder = objFSO.GetFolder(strFolderPath)
' loop through all files in the folder
For Each objFiles In objFolder.Files
' check if file extension matches
If Right(
objFiles.Name, Len(strExtension)) = strExtension Then
' create new file name with new extension
strFile = objFiles.Path
strNewFile = Left(strFile, Len(strFile) - Len(strExtension)) & strNewExtension
' create Excel object
Set objExcel = CreateObject("Excel.Application")
' open workbook
Set objWorkbook = objExcel.Workbooks.Open(strFile)
' save as XLSX file
objWorkbook.SaveAs strNewFile, 51
' close workbook and Excel object
objWorkbook.Close False
objExcel.Quit
' release Excel objects
Set objWorkbook = Nothing
Set objExcel = Nothing
End If
Next
' release file system objects
Set objFiles = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
WScript.Echo "All XLS files converted to XLSX."
```