Bat file to Search CSV files, and add up Matl

January 15, 2010 at 14:53:47
Specs: Windows XP, 3GB RAM
I need a quicker way to get a material order together. I figure I can use the CSV txt file generated by our CAM program. It generates a CSV file named CNCRunXX.txt (where XX represents the run number) at the same time it generates the run programs for the CNC machine. The CNCRunXX.txt files contain CSV strings where the 6th value is the material name. If I could use a batch file (or other method- feel free to suggest something else) to search all the CNCRun.txt files in a folder or directory, then add up all the instances of a material, and then generate a total for each material, then I'd be a happy camper. It sure would be a lot easier than counting it by hand.

Any suggestions?

Thanks for the help!


See More: Bat file to Search CSV files, and add up Matl

Report •

#1
January 15, 2010 at 15:17:59
Quick, Dirty, and Untested:
for /f "delims==" %%a in ('set __') do set %%a=
for %%A in (CNCRun*.txt) do for /f "tokens=6* delims=," %%a in (%%A) do set /a __%%a += 1
for /f "tokens=1* delims=_=" %%a in ('set __') do @echo %%b %%a


Report •

#2
January 15, 2010 at 19:12:07
Thanks Razor...
Haven't you helped me before?

Now I'm wishing for a line by line explanation. Meanwhile I'll be looking at the help files to try and decipher this, and then try it on some txt files...

Thanks for the help!


Report •

#3
January 15, 2010 at 21:01:20
not intending to be intrusive, just trying to help (it clarifies my own mind somwhat)
SET nn
(in this case, "nn" is __)
yields all var.s currently existing (local or global) that begin with nn, so
first step, wipe out all the ones he's going to use (__xxxx)
that way no existing ones can conflict with the local arena.
next, for each file, for each line, for column 6 (determ by ","):
the contents of column 6 become a var.name (preceded by __ to set them
off from all the other environment vars). f/e: col. 6 in context: 1,2,a,b,z,AA,blah
creates and/or increments a var called __AA whose value incr. by 1 each time
AA is encountered.
finis: just display all var.s beginning with __ (I would have cheated, and just said:
SET __
which would have yielded display like:
__AA=23
__BB=2
but he was more polite than me, so he cleaned it up:
AA 23
BB 2
(notes: null delimited tokens are NOT passed as null. so f/e:
tokens=1,2,3 delims=,"
inputline is:
,,2
does not yield the intuitive result of nul for %%a, nul for %%b, 2 for %%c,
it yields 2 for %%a!
I have no idea why they did it this way, any more than why they had batch to
skip null lines. null is data (at least, if it's delimited by something), just like anything else! why discrminate? it would
have been MUCH better to have done it the other way im(uneducated)Opin.
Now we are reduced to using "find /n" to grab null lines, and idontknowwhat
to grab null-data-tokens. end-of-rant, apologies. (also aplg. for length of post)

Report •

Related Solutions

#4
January 16, 2010 at 05:13:28
I have no idea why they did it this way
Remember, FOR defaults its delims to whitespace, and compacting multiple spaces into one makes sense.

As for the empty lines, I've always assumed it was part of the same compacting of delims, only in this case, the delim is the new line characters.


Report •

#5
January 16, 2010 at 11:36:50
yeah, i guess i can understand that for the default delims of space/tab, but it would have been nice if they would have excepted this behavior when delimiter is a "hard" char. like comma etc.
(or better yet, made it into an option of "for")

Report •

Ask Question