computing
  • 0

Read From CSV File Using Batch Script

  • 0

I have csv files with 4 rows:
john,16,male,9898989898
tom,19,male,8787878787
tia,22,female,6767676767

if i used below command:
@echo off
cls
setlocal enabledelayedexpansion

set /p header=<test.csv

for /f “skip=1 tokens=*” %%A in (test.csv) do (
set inline=%%A

for /f “tokens=1-4 delims=,” %%1 in (“!inline!”) do (
set col1=%%1&set; col2=%%2&set; col3=%%3&set; col4=%%$
echo %%1,%%2,%%3,%%4>output2.csv

)
)

type output2.csv

The result will be:
tia,22,female,6767676767

what if i have 100 lines and i want to display maybe few lines?

i want to display 4 columns but i want to display like this:

First person = john,16,male,9898989898
Second person = tom,19,male,8787878787
Third person = tia,22,female,6767676767

any help?

Share

1 Answer

  1. change this line:
    echo %%1,%%2,%%3,%%4>output2.csv

    to:
    echo %%1,%%2,%%3,%%4>>output2.csv

    be sure to “clear” output2.csv prior to each run unless you want incremental additions.

    • 0