Pass more than 9 arg while reading csv in batch script

May 5, 2020 at 12:56:43
Specs: Windows 7
if i used below command:
@echo off
setlocal enabledelayedexpansion

set /p header=<test.csv

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

for /f "tokens=1-12 delims=," %%1 in ("!inline!") do (
set col1=%%1&set col2=%%2&set col3=%%3&set col4=%%4&set col5=%%5&set col6=%%6&set col7=%%7&set col8=%%8&set col9=%%3&set col10=%%10&set col11=%%11&set col12=12%%&set

echo ^< Name=%%1,ID=%%2,EMPID=%%3,Location=%%4,Des=%%5,Mob_No=%%6,Gender=%%7,Birthdate=%%8,maritial status=%%9,experience=%%10,degree=%%11,collage=%%12/^> >>output2.XML


After running above script The Result is:

Name=Manjiri,ID=12,EMPID=A2667,Location=Mumbai,Des=manager,Mob_No=888992233,Gender=Female,Birthdate=25,30/10/1994,maritial status=Married,experience=Manjiri ,degree=12,collage=A2667
Name=Sourabh,ID=13,EMPID=A337,Location=Pune,Des=developer,Mob_No=7733067431,Gender=male,Birthdate=29,11/12/1990,maritial status=Married,experience=Sourabh ,degree=13,collage=A337

The exapected result is as below:

Name=Manjiri,ID=12,EMPID=A2667,Location=Mumbai,Des=manager,Mob_No=888992233,Gender=Female,Birthdate=25,30/10/1994,maritial status=Married,experience=3 Year,degree=MBA,collage=Symbiosis
Name=Sourabh,ID=13,EMPID=A337,Location=Pune,Des=developer,Mob_No=7733067431,Gender=male,Birthdate=29,11/12/1990,maritial status=Married,experience=5 Year ,degree=ME,collage=Symbiosis

the problem is When it goes to %%10 it read column 1 vaue, %%12= read column 2 value

I tried SHIFT but it not works in for loop.

Any Help???

See More: Pass more than 9 arg while reading csv in batch script

Reply ↓  Report •

May 5, 2020 at 18:56:33
Ive never done this in Batch Script before.

Have you tried to not use "&" and just have them on separate lines?

set col1=%%1
set col2=%%2

Is it a must to do this in CMD? I would recommend checking out PowerShell.
If you explain a bit more what you're trying to do I could get you started with PowerShell.

Reply ↓  Report •

May 6, 2020 at 11:28:00
Yes i have tried that but noluck.

I want to read CSV file and it contain more than 50 columns by using batch script and convert into XML file But XML tags should not open and closed with each column and row value

for ex : if i have csv format is shown below
Name age emp_no
Manjiri 25 A2449
Sourabh 30 A2448

I want to convert it in following XML format
<Name= Manjiri age=25/>
<Name= Sourabh age=30/>

so i was trying to archive this by using echo

But yes we can use PowerShell.

Please help

message edited by Manjiri

Reply ↓  Report •

May 6, 2020 at 12:29:54
Just wonder why you want Name and Age in same tag.
However if youre just looking to convert a CSV file to XML there is a simple command for that:

import-csv -path "folder\file.csv" | export-clixml -path "folder\file.xml"

To read the xml you can use:

import-clixml -path "folder\file.xml"

Reply ↓  Report •
Related Solutions

Ask Question