jftuga Posted August 20, 2007 Share Posted August 20, 2007 (edited) I have found that I need this functionality every now and then and this batch file is quicker than having to dump data into an Excel spreadsheet. I posted it here because others may find it useful, too. -John@echo offrem sumcol.batrem -John Taylorrem Aug-20-2007remrem Computes the numeric sum of a given column of datarem mawk.exe is from: http://www.klabaster.com/freeware.htm#mawkremrem Example:rem cd c:\windows\system32rem dir *.dll | findstr /i /e ".dll" | sumcol 4rem ----rem now compare this to the output of:rem dir *.dll | findstr "File(s)"setlocalset COL=%1set FNAME=%2if not defined COL goto USAGE:RUNrem for debugging...rem mawk.exe "{ gsub(/,/, '', $%COL%); SUM += int($%COL%); print NR' 'SUM} END { print 'sum: 'SUM }" %FNAME%mawk.exe "{ gsub(/,/, '', $%COL%); SUM += int($%COL%) } END { printf('%%15.0f\n', SUM) }" %FNAME%goto END:USAGE@echo.@echo Usage:@echo %0 [ column # ] [ filename ]@echo column numbers start at 1@echo filename is optional, this will otherwise read from standard-in@echo.@echo you can also use NF for the column #, which means the last column@echo (NF-1) means the 2nd to the last column, (NF-2) means the 3rd to the last column, etc.@echo.goto END:ENDendlocal Edited August 23, 2007 by jftuga Link to comment Share on other sites More sharing options...
jaclaz Posted August 21, 2007 Share Posted August 21, 2007 (edited) Nice , but if I may, the same can be made in "pure" batch, as long as data in the chosen column are integers.A quick example:file dirlist.cmd:@ECHO OFFSETLOCAL ENABLEEXTENSIONSdir /-C %1 | find /i "%~x1" >dir.txtfile addsize.cmd@ECHO OFFSETLOCAL ENABLEEXTENSIONSSET TOTAL=rem SET COL=%1rem SET FNAME=%2FOR /F "tokens=%1" %%A in (%2) DO CALL :addsize %%AECHO TOTAL is %TOTAL%GOTO :EOF:addsizerem SET size=%1SET /A TOTAL=%TOTAL%+%1GOTO :EOF(if data is "space" or "tab" separated, otherwise one needs to add a delims= statement in the FOR loop)Run:dirlist.cmd c:\windows\system32\*.dlland then:addsize.cmd 3 dir.txtjaclaz Edited August 21, 2007 by jaclaz Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now