Batch to add the column in a txt file

July 8, 2009 at 11:24:39
Specs: Windows XP
Hi, I need to write a batch program to read the file and calculate the Credit Amount column.

my txt file looks like this:
Originator Unit Unit Credit Amount Ref# Account
343434 23232 20090607 450 1 R1
343435 23233 20090607 890 4 R3
343436 23234 20090607 310 3 R5

I need to add the Credit Amount (450+890+310). The amount should be with leading zeros: 000000001650.00. So the total length is 15 digits including decimals. Please help me how to read the Credit Amount and then add the total with leading zeros and decimals.


See More: Batch to add the column in a txt file

Report •


#1
July 8, 2009 at 12:13:23
Batch math only does integers. No decimals.


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

M2


Report •

#2
July 8, 2009 at 15:38:31
Hi b1pal

Where's the decimals, If there were there they could be
calculated, but you could not do any more maths with the result, only display it.
As Mechanic says there's only integer maths in batch.




Report •

#3
July 8, 2009 at 23:07:08
here's a vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
total = 0
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	fields = Split(strLine," ")
	total = total + fields(3)
Loop

Function Pad(input, howmany)
	'Pad leading zeroes to a string till length of howmany		
	Do Until Len(input) = howmany
		input = "0" & input
	Loop
	Pad = input
End Function
WScript.Echo Pad(FormatNumber(total,2,,,0),15)

GNU win32 packages | Gawk


Report •

Related Solutions

#4
July 9, 2009 at 09:00:35
Hi, I am not suppose to use vbscript only batch. If the decimal can not be done, its ok. Can we at least get the total of "Credit Amount" column? I have no clue how to add a column in the batch script.

Thank You


Report •

#5
July 9, 2009 at 11:47:58
@echo off & setLocal enableDELAYedexpansion

set CA=

for /f "skip=1 tokens=4 delims= " %%a in (myfile) do (
set /a CA+=%%a
)
call :sub1
echo total credit amt is !CA!

goto :eof

:sub1
:loop
if "!CA:~14,1!" equ "" (
set CA=0!CA!
goto :loop
)

goto :eof


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

M2


Report •

#6
July 10, 2009 at 09:24:30
Hi,

I did exactly what you said but it does not add and not sure why. 15th is the column name so I added tokens=15.

set CA=

for /f "skip=1 tokens=15 delims= " %%a in (Newfile.log) do (
set /a CA+=%%a
)

echo ca is !CA!

If I remove the delims=
for /f "skip=1 tokens=15" %%a in (Newfile.log) do (
echo a is %%a
)
I see all the numbers echo a is 450
echo a is 300,..

But when I add set /a CA+=%%a it just goes in a loop and does not add the numbers. ca is (blank). Not sure what's wrong. Thanks


Report •

#7
July 10, 2009 at 11:17:12
Post your bat and your file separately.


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

M2


Report •

#8
July 10, 2009 at 11:31:09
Hi, here is my bat file.

@echo off & setLocal enableDELAYedexpansion

:today
set today=%DATE:~10,4%
set today=%today%%DATE:~4,2%
set today=%today%%DATE:~7,2%

echo.

:main
for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (

set input=%%a
set filename=!input!

SET filename=!filename:~0,-13!
TYPE %%a >> Newfile.log


FOR /F "TOKENS=1" %%y in (Newfile.log) DO SET /A R=R+1

rem find the length of the rows
set /a R=!R!+2
set #=%R%
set length=0
:loop
if defined # (
set #=%#:~1%
set /A length += 1
call :loop)


rem AddZeros total zeros are 9
set Len=!length!
set Zeros=9
set /a NoZeros=!Zeros!-!Len!
for /L %%a in (1,1,!NoZeros!) do (
set Num=0!Num!

)

set hdr=HDR%!today!!filename!
set trl=TRL%!Num!!R!

echo trl is !TRL!
set CA=

for /f "skip=1 tokens=15" %%a in (Newfile.log) do
(
set /a CA+=%%a
)

:loop
if "!CA:~14,1!" equ "" (
set CA=0!CA!
goto :loop
)

echo CA is !CA!

ECHO !HDR! >> C:\AMEX\Batch\Outbound\ChargeBack\Datefile.log

ECHO !TRL!!CA! > C:\temp\TrlFile.log
TYPE Newfile.log >> Datefile.log

TYPE TrlFile.log >> Datefile.log

del TrlFile.log
del Newfile.log
COPY Datefile.log C:\temp\ChargeBack
del Datefile.log
cd C:\temp\ChargeBack
Datefile.log !input!
set filename=
set hdr=
set trl=
set input=
set num=
set len=
set length=
set R=
set NoZeros=
set CA=
cd C:\temp

)
::== DONE
goto :eof


Report •

#9
July 10, 2009 at 11:31:54
file:

Originator Unit Unit Curr CC& GEO Account Sub Account DEBIT Amount Reference# Line Description Unit Curr CC& GEO Account Sub Account CREDIT AMOUNT Reference# Line Description IC Description Doc Reqd.
343434 23232 20090607 1 34 34a 35c 66 TRS R1 20090607 1 34 34a 300 66 TRS Credit dd
343435 23233 20090607 2 67 67a 68c 67 BAM R2 20090607 2 67 67a 400 67 BAM Credit dd
343436 23234 20090607 3 78 78a 79c 78 606 R3 20090607 3 78 78A 650 78 606 Credit dd


Report •

#10
July 10, 2009 at 12:30:42
I have no idea what most of that is doing, but this chunk:
============================================
for /f "skip=1 tokens=15" %%a in (Newfile.log) do
(
set /a CA+=%%a
)
==========================================
should be:

for /f "skip=1 tokens=15" %%a in (Newfile.log) do (
set /a CA+=%%a
)


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

M2


Report •

#11
July 10, 2009 at 12:53:45
Hi b1pal

If your code had decimals like this use the code below,
otherwize rem our the line set /a Dec+=%%e as stated below.

If textfile had decials
Originator Unit Unit Credit Amount Ref# Account
343434 23232 20090607 450.90 1 R1
343435 23233 20090607 890.90 3 R3
343436 23234 20090607 310.95 3 R5

No Decimals
Originator Unit Unit Credit Amount Ref# Account
343434 23232 20090607 450 1 R1
343435 23233 20090607 890 3 R3
343436 23234 20090607 310 3 R5


@echo off
SetLocal EnableDelayedExpansion

set Sum=0
set Dec=0
for /f "skip=1 tokens=1-7 delims=. " %%a in (sum.txt) do (
set /a Sum+=%%d
::----------------------------
rem out line below, if there's no decimals
::----------------------------
set /a Dec+=%%e
echo !Sum!.!Dec!
)
set /a Sum=%Sum%*100+%Dec%
set Dec=%Sum:~-2%
set /a Sum=%Sum%/100
set Sum=%Sum%.%Dec%
echo --------
echo %Sum%
echo --------
call :AddZeros %Sum% 15
echo %StrNum%
exit /b

:AddZeros %1 %2
call :StrLen %1
set StrNum=%1
set NumLen=%Len%
set StrLen=%2
set /a NoZeros=%StrLen%-%NumLen%
for /L %%a in (1,1,!NoZeros!) do (
set StrNum=0!StrNum!
)
exit /b


:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" (
set /a Len=%%a+1
) else (exit /b)
)
exit /b


Report •

#12
July 10, 2009 at 13:26:59
Hi,

Yes, I do have decimals and it is going to be on the 15th column so this is what I did from your code below:

set Sum=0
set Dec=0
for /f "skip=1 tokens=15 " %%a in (sum.txt) do (
set /a Sum+=%%d
)

for /f "skip=1 tokens=15 delims=. " %%a in (Newfile.log) do (
set /a Dec+=%%e
)

echo !Sum!.!Dec!

When echo above , I only get 1350.0. So that works but it needs 1350.00

I don't know what this suppose to do:
set /a Sum=%Sum%*100+%Dec%
set Dec=%Sum:~-2%
set /a Sum=%Sum%/100
set CA=%CA%.%Dec%

It gave me missing operand error.
Thanks


Report •

#13
July 10, 2009 at 15:16:03
Hi b1pal

Well just add another zero
echo !Sum!.!Dec!0

or Set Num=!Sum!!Dec!0
echo !Sum!

That code calculates the decimal sum
I think you need to study batch script a lot more or try and learn things from what code other people are have given you.



Report •

#14
July 14, 2009 at 12:39:01
Hi,

for /f "skip=1 tokens=15 delims=. " %%a in (Newfile.log) do (
set /a Dec+=%%e
)

The Dec is 0 from above.
The total should be (200.60+400.40+250.20) =851.20

The Sum is coming as 850.

set /a Sum=%Sum%*100+%Dec%
set Dec=%Sum:~-2%
set /a Sum=%Sum%/100
set Sum=%Sum%.%Dec%

The Sum from above code is 850.0

That's why I was asking what the code is doing. Should the Dec be calculated as 1.20?


Report •


Ask Question