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 R5I 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.

Batch math only does integers. No decimals.

=====================================

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

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.

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)

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

@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

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

Post your bat and your file separately.

=====================================

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

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+1rem 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.logTYPE 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

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

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

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 R5No 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 EnableDelayedExpansionset 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

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

Hi b1pal Well just add another zero

echo !Sum!.!Dec!0or 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.

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.20The 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?

Ask Your Question

Weekly Poll

Do you think Monopoly should update its pieces?

Discuss in The Lounge

Poll History