It's my first time programming with batch files and I was asked to do a program capable of converting .xlsm files into .csv, without having to open Excel to do so. To do so, I use this .bat file:
extoc.vbs integration.xlsm integration.csv
Taskkill /IM EXCEL.EXE /F
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
call :StripBlankLines "integration.csv"
goto :eof
:StripBlankLines
For %%x in ("%~1") do set OutF=integration_er.csv
if exist "%OutF%" del "%OutF%"
set FirstLine=1
for /F "usebackq delims=" %%B in (%*) do (
call :TrimWS %%B
if not "!Line!"=="" (
if "!FirstLine!"=="1" (
set FirstLine=0
) else (
>>"%OutF%" echo.
)
call :write !Line!
)
)
goto :eof
:TrimWS
set Line=%*
goto :eof
:write
>>"%OutF%"<NUL set /p Dummy=%*
goto :eof
Together with the .vbs file:
if WScript.Arguments.Count < 2 Then
WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>"
Wscript.Quit
End If
csv_format = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.SaveAs dest_file, csv_format
oExcel.Quit
oBook.Close False
The problem in here is that I get, as a final result, a giant CSV containing all the information from the original .xlsm file, but also several lines filled with nothing but commas, as you can see bellow:
data, data, data,, data, data, data, data, data, data
data, data, data,, data, data, data, data, data, data
data, data, data,, data, data, data, data, data, data
data, data, data,, data, data, data, data, data, data
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
While I need to have something like this:
data, data, data,, data, data, data, data, data, data
data, data, data,, data, data, data, data, data, data
data, data, data,, data, data, data, data, data, data
data, data, data,, data, data, data, data, data, data
I know these commas come from blank rows in excel, but I can't use a VBA Macro to remove all the blank rows in excel because it is a HUGE sheet and my PC crashes everytime I try to do it. So, if there's some way to solve this through VBS or BATCH I would be REALLY glad!
Replace(<your csv>,",,,,,,,,," & vbCrLf,"")?