Page 1 of 1

Read/find in CSV file and other simple operations

Posted: 25 Jun 2010 09:32
by ghosh.d7
hi,

still very new to batch. how would I find an entry, say a string "Schedule", in a CSV file and then set it to a variable in my batch file?

for instance, I want to:

1. find "Schedule" in the file "C:\ops\fileA.csv" (I know "Schedule" occurs in col 1 of the CSV)
2. set a string variable, say schedNum, in the batch to the entry in col 2 of the row where "Schedule" occurs
3. open another file, "C:\ops\fileB.csv" and find the row(s) in which schedNum occurs. schedNum will occur in the first col.
4. In the row where schedNum occurs, copy the entries in col 2 and col 3 to two other integer variables in the batch, line1 and line2
5. open a database file "C:\ops\database.mdb" and open a table in it, "Table1"
6. In Table1, add a new row at the end of the table (after the last row that has an entry). Set col1 and col2 to line1 and line2

And I want to be able to do this for several entries (for instance, Schedule1, Schedule2, etc.

I know this is a lot! Since I don't really know the correct commands/syntax, I'm hoping that if someone can help me out then I'll know this stuff next time around.

Thanks a lot

Re: Read/find in CSV file and other simple operations

Posted: 25 Jun 2010 14:06
by aGerman
Four and a half of your issues could be done very easy. But open a specific table, find the last row and append data? ... I'm virtually sure that you have to write a macro in your data base to do this.

Regards
aGerman

Re: Read/find in CSV file and other simple operations

Posted: 28 Jun 2010 06:13
by ghosh.d7
Hi,

What would be the syntax for the first step, finding a string in the first column of a CSV?

Thanks

Re: Read/find in CSV file and other simple operations

Posted: 28 Jun 2010 11:27
by aGerman
Idk which csv settings are valid for you. So I hope comma is the delimiter for the values (btw. it's semicolon for my German settings).

Code: Select all

@echo off &setlocal
for /f "delims=, tokens=2" %%a in ('findstr /b /c:"Schedule," "C:\ops\fileA.csv"') do set "schedNum=%%a"
set /a n=0
for /f "delims=, tokens=2,3" %%a in ('findstr /b /c:"%schedNum%," "C:\ops\fileB.csv"') do (
  set /a n+=1
  call set "line1_%%n%%=%%a"
  call set "line2_%%n%%=%%b"
)
echo %n% rows found
echo values:
set line1_
set line2_
pause



Regards
aGerman

Re: Read/find in CSV file and other simple operations

Posted: 28 Jun 2010 13:28
by ghosh.d7
Thank you very much, this worked for me. I should be able to handle the rest.