Solved Ignore empty cell value while reading CSV

May 18, 2020 at 06:46:23
Specs: Windows 7
I am pretty new to batch file scripting.

I have to read data from the CSV file using batch script.

But now struck with one more hurdle.

While reading CSV file If any cell contain null value i want to ignore that value and if cell contain any data i want to ECHO that

Batch script i am using is as follow

@echo off & setlocal
set source=test.csv
set xml=test.xml
del %xml% 2>nul
set pre=""
rem ----- get labels
set /p h=<%source%
set c=1
call :aa %h%
goto :eof

:aa
set k%c%=%~1
set /a c+=1
shift
if "%1" neq "" goto :aa
for /f "skip=1 tokens=1* delims=," %%a in (%source%) do call :xx %%a %%b
goto :eof

:xx
if %1 neq %pre% (
if %pre% neq "" >> %xml% echo ^</Department^>
>> %xml% echo ^<department = %~1^>
set pre=%1
)
>>%xml% echo ^<%k2%=%~2 %k3%=%~3 %k4%=%~4 %k5%=%~5 %k6%=%~6 %k7%=%~7 %k8%=%~8 %k9%=%~9

SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
>>%xml% echo ^<%k2%=%~2 %k3%=%~3 %k4%=%~4 %k5%=%~5 %k6%=%~6 %k7%=%~7 %k8%=%~8 %k9%=%~9

goto :eof


so after shift while reading %k6%=%~6 %k7%=%~7 this value first i want to check that is it null or not.
if it is null then i want to ignore it,dont want to print any thing but if contain any value i want ECHO %k6%=%~6 %k7%=%~7

ANY HELP???

For ex

input CSV
Department Name ID_No City
HR Sneha 45 Satara
HR Riya pune

desired output:
department = "HR" NAME="Sneha" ID_NO="45" City="Satara"
department = "HR" NAME="Riya" ID_NO=" " City="Pune"

Expected Output
department = "HR" NAME="Sneha" ID_NO="45" City="Satara"
department = "HR" NAME="Riya" City="Pune"


See More: Ignore empty cell value while reading CSV

Reply ↓  Report •

#1
May 19, 2020 at 18:18:23
✔ Best Answer
might try this:
@echo off & setlocal enabledelayedexpansion
set source=test.csv
set xml=test.xml
del %xml% 2>nul

rem ----- get labels
set /p h=<%source%
set c=1
call :aa %h%
goto :eof

:aa
if "%1" equ "" goto :begin
set k%c%=%~1
shift
set /a c+=1
goto :aa

:begin
set /a no.fields=c-1
for /f "skip=1 tokens=* delims=," %%a in (%source%) do call :xx "%%a"
goto :eof

:xx
set z=%~1
set z=%z:,,=,"",%
echo %z%| findstr "^,">nul&&set z=""%z%
echo %z%| findstr ",$">nul&&set z=%z%""
echo z: %z%

set out=
set c=0
call :c %z%
if defined out >>%xml% echo ^<%out%^>
goto :eof

:c
if %c% equ %no.fields% goto :eof
set /a c+=1
if "%~1" neq "" set out=%out% !k%c%!="%~1"
shift
goto :c



Reply ↓  Report •
Related Solutions


Ask Question