Showing posts with label batch. Show all posts
Showing posts with label batch. Show all posts

Thursday, 18 September 2008

recursive for loops

problem: for example, reading a text file input, searching for the entry in a database and outputting specified csv's

what im not doing: hitting this with vbs, because vbs is only for when batch cant do it


try it, recursive for loops arent supported in winxp cmd / batch files, but here is a wee workaround

for /f %%a in (list.csv) do call :list %%a
:list
if not %1'==' for /f "tokens=1-5* delims=," %%a in ('findstr /i %1 db') do echo %%~c,%%~d,%%~e >> summary.csv

save the batch and watch it recurse!

Wednesday, 12 March 2008

more batch goodness!

problem: app generated csv encases entries with double quotes, we want the double quotes removed... please!

what im not doing: using wsh (the client googled and suggested that i use vbs, looking nervous as they obviously didnt know what this meant).

start at the start, batch as always (i hate myself);

for /f "tokens=1* delims=," %a in (foo.sv) do echo %~a,%~b >> bar.csv

kinda easy!

Monday, 3 March 2008

batch magic!

problem: client has a generated csv of months credit notes issued, each item credited should be (but sometimes not due to human error) flagged as damaged, quality issue, shortage, not-as-ordered or error (*d*, *q*, *s*, *nao*, *e*). a summary of each catergory should be generated for that month. during the brief i offered a futher category of 'misc' for items not flagged and was hailed as the hero i am (for a short while).

what im not doing: playing football tonight probably... it has snowed!

batch solves the above, toffee!

setlocal enabledelayedexpansion
copy foo.csv cred.csv
set foo=foo
for %%a in (d,q,nao,s,e) do (
type !foo!.csv find /i "*%%a*" >> %%a.csv
type !foo!.csv find /i /v "*%%a*" >> "%%a%%a.csv"
del !foo!.csv
set foo=%%a%%a
)
rename ee.csv misc.csv

notes on the 'cdrill.bat'
name the initial file 'foo.csv'
need setlocal or the variable expands to 'e' instantly
piping output of type for tidyness (no ----- blah.csv at head of file as when 'find' is used), pipe doesnt seem to show up in post