Read/find in CSV file and other simple operations

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ghosh.d7
Posts: 4
Joined: 24 Jun 2010 13:50

Read/find in CSV file and other simple operations

#1 Post by ghosh.d7 » 25 Jun 2010 09:32

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 25 Jun 2010 14:06

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

ghosh.d7
Posts: 4
Joined: 24 Jun 2010 13:50

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

#3 Post by ghosh.d7 » 28 Jun 2010 06:13

Hi,

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

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 28 Jun 2010 11:27

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

ghosh.d7
Posts: 4
Joined: 24 Jun 2010 13:50

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

#5 Post by ghosh.d7 » 28 Jun 2010 13:28

Thank you very much, this worked for me. I should be able to handle the rest.

Post Reply