parse csv from a batch file

January 25, 2011 at 08:46:20
Specs: Windows XP
Gurus
Is it possible to parse a csv file using a batch file? When I run the .bat file, user is asked enter a userid in command line. All the userids and usernames are stored in a csv file comma seperated, 1 line for each userid.
Depending on the user id entered, the username for that corresponding userid should be returned.

csv file looks like this:
tom smith,tom
john smith,john

c:\
enter userid: tom

if user enter tom, "tom smith" should be returned to console


See More: parse csv from a batch file

Report •


#1
January 25, 2011 at 10:21:09
@echo off & cls
set /p "_Uname= Enter the user name :
for /f "tokens=1,2* delims=," %%a in ('findstr /i "%_Uname%" yourcsvfile.csv') do echo %%a

Subhash Chandra.
www.s-chandra.co.cc


Report •

#2
January 25, 2011 at 10:32:18
thanks subhash..this is awesome..
if i have multiple instances of the same user, can i print only the first/last occurence (basically only occurence)

also, is it possible to print the user entered parameter and the corresponding value retrieved from csv to a text file?

appreciate your help.


Report •

#3
January 25, 2011 at 13:35:59
set /p "_userid= Enter the user name :
for /f "tokens=1,2* delims=," %%a in ('findstr /i "%_userid%" ClassUsers2.csv') do echo "%%a" > myfile.txt
echo "%%a" >> myfile.txt

I am able to write this to the file correctly in the first line of the file. but, i need to write this into 2 lines on the same file..my above code prints

"tom smith"
"%a"

how can i get to print "tom smith" in the second line also.


Report •

Related Solutions

#4
January 26, 2011 at 06:43:07
Hello Gurus
How can i find an exact match of the string..say in my csv i have the below

tom smith,tom
tommy smith,tom1

With the following code, I cannot find exact match. If I enter tom, sometimes it prints tom and sometimes tom1. I tried to use /x command but that didnt work either..

@echo off & cls
set /p "_Uname= Enter the user name :
for /f "tokens=1,2* delims=," %%a in ('findstr /i "%_Uname%" yourcsvfile.csv') do echo %%a


Report •

#5
January 26, 2011 at 10:56:37
@echo off & cls
set /p "_Uname= Enter the user name :
for /f "tokens=1,2* delims=," %%a in ('findstr /i "%_Uname%" yourcsvfile.csv') do ( echo You Entered %_Uname%
echo The user name is %%a

To find the exact match use /x after /i inside the loop..
IF not working then let us know the exact error and few lines from your csv file as well

Subhash Chandra.
www.s-chandra.co.cc


Report •

#6
January 26, 2011 at 14:27:55
CSV file is
jay black,jblack
joe black,jblack

output: the user name is %%a

Actually, I figured out something so that it is unique and I dont get into this scenario. My new problem is, I want to 2 lines of text printed to a output file..

set /p "_id= Enter the id :
for /f "tokens=1,2* delims=," %%a in ('findstr /i "%_id%" csvfile.csv') do echo my first line output is %%a \n
my second line output is %%a > myfile.txt

I want the second line printed on a new line and the above code actually does it. but the problem is it prints the '\n' as well and i do not want this.


Report •

#7
January 26, 2011 at 16:53:44
if you enter id as "tom", and you have

tom jones, black
jones tome, jblack


the batch using findstr like that will return both these lines as well, BUT the id is not tom. This is undesirable, right? in this case, you should also check your %%b for correct user id.

...
for /f "tokens=1,2* delims=," %%a in ( 'type csvfile') do (
   check for %%b whether is equal to %_id%
   if yes, get your username at %%a
)

GNU win32 packages | Gawk


Report •

#8
January 26, 2011 at 19:12:28
@rkishore

Just remove \n from your code. it wont get printed. Infact \n is not a command in windows dos, its in C langauge.

Batch will print each line of echo as a new line.

Or an example.

set /p "_id= Enter the id :
for /f "tokens=1,2* delims=," %%a in ('findstr /i "%_id%" csvfile.csv') do (
echo my first line output is %%a
my second line output is %%a > myfile.txt
)

Subhash Chandra.
www.s-chandra.co.cc


Report •

#9
January 31, 2011 at 08:31:19
thanks guys. this helped me a lot.

Report •


Ask Question