Computing.Net > Forums > Programming > Help with Bat File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with Bat File

Reply to Message Icon

Name: Sandie
Date: April 28, 2009 at 02:09:59 Pacific
OS: Windows Server 2003
Subcategory: Batch
Comment:

Hi,

I have a csv file having close to 59 columns delimited with comma. I might contain some fileds with a comma in it like below:
cat,bat,tom
"bot,inc",tom,bot

the field having a comma would be enclosed in a double quote. while parsing it, i am facing problems. I want to move such lines into a different file and proceed with my process.

So, I want to count my number of commas in a line. If it is 58, I want to retain that line else i want to move that line to a different file. How can i do that in a bat file? Please help me.

Sandie



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: April 28, 2009 at 02:42:50 Pacific
Reply:

@echo off
type "My_File.csv" | find """" > "My_File_A.csv"
type "My_File.csv" | find /V """" > "My_File_B.csv"

Where "My_File_A.csv" holds the lines with double quotes while "My_File_B.csv" all the others.

0

Response Number 2
Name: Sandie
Date: April 28, 2009 at 02:49:54 Pacific
Reply:

But i do have records which are also as follows:

"bat",tom,cat
"bat,inc",tod,cot

So in turn, every line will have double quotes.
I just want to count the commas in a line.


0

Response Number 3
Name: IVO
Date: April 28, 2009 at 03:07:54 Pacific
Reply:

@echo off & setlocal EnableDelayedExpansion
for /F "delims=" %%j in ('type "My_File.csv"') do (
  set line=%%j
  set line=!line:"=!
  set line=!line: =!
  set line=!line:;=!
  set cnt=0
  for %%k in (!line!) do set /A cnt+=1
  if !cnt! gtr 59 (
    echo.%%j>> "My_File_A.csv"
    ) else (
    echo.%%j>> "My_File_B.csv"
  )
)



0

Response Number 4
Name: Sandie
Date: April 28, 2009 at 03:25:11 Pacific
Reply:

Hey IVO,

I do not see any command counting the commas or anything. what does it do?

Sandie


0

Response Number 5
Name: IVO
Date: April 28, 2009 at 03:42:07 Pacific
Reply:

Ciao (Hi) Sandie,

the counting is performed by the inner for loop, i.e.

   for %%k in (!line!) do set /A cnt+=1

Each line is cleaned removing " ; and spaces then feeds the for loop that counts the items (columns) as that statement acts on "set of strings" delimited by commas. For kind of precision the = is a separator too but I assume there are no one in your csv's row.

Anyway the script is no harmful at all. Try and report the result. As I said that counts the columns so the value of 59.


0

Related Posts

See More



Response Number 6
Name: Sandie
Date: April 28, 2009 at 03:50:21 Pacific
Reply:

Works like a charm.
Thanks a lot IVO.

I've tried with the following example file and it worked.
"1cat",tom,bat,baf
"2cat,inc",tot,cot,fom
3cal,bal,fil,jil
4cat,for

I've changed the "gtr 59" to "equ 4". I want exactly 59 columns to be checked so the equal to.

Thanks a lot for your help.


0

Response Number 7
Name: Sandie
Date: April 28, 2009 at 04:54:00 Pacific
Reply:

IVO,

I am having trouble with the following type of records:
"cat",,bat,tot
"cat","bat",,tom

any solutions for this case???


0

Response Number 8
Name: Sandie
Date: April 28, 2009 at 04:58:02 Pacific
Reply:

Never mind.. i got it..

i added the following line
set line=!line:,,=,EMPTY,!


0

Response Number 9
Name: Mechanix2Go
Date: May 1, 2009 at 04:45:54 Pacific
Reply:

Hi Sandie, nice move.

Hi IVO,

I worked on this a few days ago and was trying to test each char for equ comma. As usual, your solution is better.


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with Bat File

Help with bat file www.computing.net/answers/programming/help-with-bat-file/12224.html

Need help with bat file www.computing.net/answers/programming/need-help-with-bat-file/13270.html

I need help with bat file - please help www.computing.net/answers/programming/i-need-help-with-bat-file-please-help/2971.html