I'm trying to convert multiple Excel files (xls) to csv using the following powershell script:
$excel = new-object -ComObject "Excel.Application"
$excel.DisplayAlerts=$True
$excel.Visible =$false
foreach ($file in get-childitem $src_dir) {
$wb = $excel.Workbooks.Open($file.FullName)
$wb.SaveAs($dst_dir + $file.Name + ".csv", 6)# 6 -> csv
$wb.Close($True)
}
$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
In principle this works e.g. I get csv files. However, for a few files (varying per run) I get an exception:
Exception calling "SaveAs" with "2" argument(s): "Microsoft Office Excel cannot access the file 'C:\Users\...\AppData\Local\Temp'. ...
Additionally, I get a message box asking if I want to save the changes to the source xls.
Once I call SaveAs, $wb references the new file. So how do I save or discard the changes to the source file? Why does this happen only for a few files? Are there any other problems with this script?
Update
I divided the input files (ca. 200) arbitrarily (i.e. don't know the size of the groups) into 10 groups and processed each group in its own run. That worked so it is somewhat inconvenient.
thanks in advance