使用批处理脚本连接相同名称的内容并将每个内容的值相加

2024-04-05

几年前@Mofi 帮助我创造了一个令人惊叹的.bat脚本对许多内容的值进行求和,还可以将其名称连接起来并求出它们的总和。

请检查旧答案,重点关注 Mofi 的答案。

如何使用批处理文件对文本进行排序和求和时忽略第一和第三数据列? https://stackoverflow.com/questions/52224428/how-to-ignore-first-and-third-data-column-on-sorting-and-summing-the-text-using

但我注意到一个问题。

  1. 低于 1,00 美元的价值总和并不正确。
  2. 大于 $99.999,99 的值总和不正确或者有时输出错误。

一个例子:

使用这个.txt要重新格式化的文件:

                     04  VERO BANRICOMPRAS A PRAZO                         651326             885,92
                         VERO BANRICOMPRAS A VISTA                         038937             470,90
                         VERO CARTAO CREDITO                               100810           2.126,17
                         VERO CARTAO CREDITO                               100811           1.354,05
                         VERO CARTAO CREDITO                               100812             148,91
                         CIELO S/A CARTAO CREDITO                          084036              45,75
                         CIELO S/A CARTAO CREDITO                          084037              24,18
                         CIELO S/A CARTAO CREDITO                          084038             108,34
                         REDECARD S.A CARTAO CREDITO                       154346             163,81
                         VERO CARTAO DEBITO                                214591           1.338,66
                         VERO CARTAO DEBITO                                214592             556,16
                         VERO CARTAO DEBITO                                214593             113,96
                         CIELO S/A CARTAO DEBITO                           186651             491,87
                         CIELO S/A CARTAO DEBITO                           186652             678,53
                     05  VERO BANRICOMPRAS A PRAZO                         976373             504,55
                         VERO BANRICOMPRAS A VISTA                         440325             397,56
                         VERO CARTAO CREDITO                               496411             725,69
                         VERO CARTAO CREDITO                               496412             545,53
                         CIELO S/A CARTAO CREDITO                          488259             135,32
                         CIELO S/A CARTAO CREDITO                          488260              33,72
                         REDECARD S.A CARTAO CREDITO                       519807              22,81
                         VERO CARTAO DEBITO                                555957             619,03
                         VERO CARTAO DEBITO                                555958             452,29
                         CIELO S/A CARTAO DEBITO                           542472             946,15
                     06  VERO BANRICOMPRAS A PRAZO                         419616             257,17
                         VERO BANRICOMPRAS A VISTA                         771632             379,49
                         VERO CARTAO CREDITO                               818228           1.317,35
                         VERO CARTAO CREDITO                               818229             708,32
                         VERO CARTAO CREDITO                               818230              84,28
                         CIELO S/A CARTAO CREDITO                          809536              14,88
                         REDECARD S.A CARTAO CREDITO                       846159              51,57
                         VERO CARTAO DEBITO                                883969             229,31
                         VERO CARTAO DEBITO                                883970              85,83
                         CIELO S/A CARTAO DEBITO                           870136              49,18
                         CIELO S/A CARTAO DEBITO                           870137              42,28
                         RESGATE AUTOMATICO                                000000           6.200,00
                     07  VERO BANRICOMPRAS A PRAZO                         742635             148,84
                         VERO BANRICOMPRAS A VISTA                         135047             111,52
                         VERO CARTAO CREDITO                               178947             867,82
                         VERO CARTAO CREDITO                               178948              41,02
                         CIELO S/A CARTAO CREDITO                          169813             133,68
                         CIELO S/A CARTAO CREDITO                          169814              24,98
                         VERO CARTAO DEBITO                                244107             596,02
                         VERO CARTAO DEBITO                                244108             364,95
                         CIELO S/A CARTAO DEBITO                           229583             651,76
                     08  VERO BANRICOMPRAS A PRAZO                         085581             274,00
                         VERO BANRICOMPRAS A VISTA                         489251             950,36
                         VERO CARTAO CREDITO                               532717             601,15
                         VERO CARTAO CREDITO                               532718             488,93
                         CIELO S/A CARTAO CREDITO                          523033              69,76
                         CIELO S/A CARTAO CREDITO                          523034              58,36
                         CIELO S/A CARTAO CREDITO                          523035              34,30
                         REDECARD S.A CARTAO CREDITO                       562700             185,53
                         VERO CARTAO DEBITO                                603103           2.608,68
                         VERO CARTAO DEBITO                                603104           1.124,42
                         CIELO S/A CARTAO DEBITO                           588005           1.419,60
                     11  VERO BANRICOMPRAS A PRAZO                         451443             770,43
                         VERO BANRICOMPRAS A VISTA                         800736           1.377,51
                         VERO CARTAO CREDITO                               859775           1.673,38
                         VERO CARTAO CREDITO                               859776             836,50
                         VERO CARTAO CREDITO                               859777              27,42
                         VERO CARTAO CREDITO                               859778              39,24
                         CIELO S/A CARTAO CREDITO                          844066              26,44
                         CIELO S/A CARTAO CREDITO                          844067              45,37
                         CIELO S/A CARTAO CREDITO                          844068              54,34
                         REDECARD S.A CARTAO CREDITO                       910400             127,17
                         VERO CARTAO DEBITO                                972748           3.560,31
                         VERO CARTAO DEBITO                                972749           1.939,33
                         CIELO S/A CARTAO DEBITO                           944629             175,81
                         CIELO S/A CARTAO DEBITO                           944630           2.161,99
                         CIELO S/A CARTAO DEBITO                           944631              58,72
                         CIELO S/A CARTAO DEBITO                           944632           1.559,94
                         RESGATE AUTOMATICO                                000000          15.500,00
                     12  VERO BANRICOMPRAS A PRAZO                         742222             159,97
                         VERO BANRICOMPRAS A VISTA                         190951           1.247,06
                         VERO CARTAO CREDITO                               334660             669,75
                         VERO CARTAO CREDITO                               334661             263,14
                         VERO CARTAO CREDITO                               334662              99,13
                         CIELO S/A CARTAO CREDITO                          325837              17,40
                         CIELO S/A CARTAO CREDITO                          325838             161,58
                         REDECARD S.A CARTAO CREDITO                       358417             106,89
                         VERO CARTAO DEBITO                                394014             128,39
                         VERO CARTAO DEBITO                                394015             349,08
                         CIELO S/A CARTAO DEBITO                           381003             892,94
                         RESGATE AUTOMATICO                                000000           6.900,00
                         DEVOLUCAO DOC. ELETRONICO                         000060           3.949,49

                     13  VERO BANRICOMPRAS A PRAZO                         147127             136,58
                         VERO BANRICOMPRAS A VISTA                         564315             225,14
                         VERO CARTAO CREDITO                               917004             656,04
                         VERO CARTAO CREDITO                               917005              84,70
                         VERO CARTAO CREDITO                               917006             103,07
                         CIELO S/A CARTAO CREDITO                          907873             190,28
                         CIELO S/A CARTAO CREDITO                          907874              65,24
                         REDECARD S.A CARTAO CREDITO                       945528             155,34
                         VERO CARTAO DEBITO                                981014             488,95
                         VERO CARTAO DEBITO                                981015             112,82
                     14  VERO BANRICOMPRAS A PRAZO                         534959              19,21
                         VERO BANRICOMPRAS A VISTA                         123609             343,33
                         VERO CARTAO CREDITO                               174070             225,84
                         VERO CARTAO CREDITO                               174071             237,41
                         CIELO S/A CARTAO CREDITO                          164650              40,68
                         CIELO S/A CARTAO CREDITO                          164651              26,54
                         VERO CARTAO DEBITO                                237408             608,63
                         VERO CARTAO DEBITO                                237409           1.061,97
                         CIELO S/A CARTAO DEBITO                           224104             232,16
                     15  VERO BANRICOMPRAS A PRAZO                         091718             212,73
                         VERO BANRICOMPRAS A VISTA                         412318             528,61
                         VERO CARTAO CREDITO                               464462           1.255,15
                         VERO CARTAO CREDITO                               464463              82,57
                         VERO CARTAO CREDITO                               464464              72,84
                         CIELO S/A CARTAO CREDITO                          454334              25,47
                         CIELO S/A CARTAO CREDITO                          454335             204,90
                         CIELO S/A CARTAO CREDITO                          454336             218,38
                         REDECARD S.A CARTAO CREDITO                       495065              81,70
                         VERO CARTAO DEBITO                                532669             340,94
                         VERO CARTAO DEBITO                                532670              63,15
                         CIELO S/A CARTAO DEBITO                           518884             699,42
                     18  VERO BANRICOMPRAS A PRAZO                         383614             691,40
                         VERO BANRICOMPRAS A VISTA                         719756             955,41
                         VERO CARTAO CREDITO                               766119           3.335,37
                         VERO CARTAO CREDITO                               766120           1.460,17
                         VERO CARTAO CREDITO                               766121             637,74
                         CIELO S/A CARTAO CREDITO                          749384             185,38
                         REDECARD S.A CARTAO CREDITO                       818055             341,85
                         VERO CARTAO DEBITO                                876849           1.686,83
                         VERO CARTAO DEBITO                                876850             623,29
                         VERO CARTAO DEBITO                                876851              24,33
                         CIELO S/A CARTAO DEBITO                           852038             157,38
                         CIELO S/A CARTAO DEBITO                           852039             892,36
                         CIELO S/A CARTAO DEBITO                           852040             818,90
                     19  VERO BANRICOMPRAS A PRAZO                         660866             118,53
                         VERO BANRICOMPRAS A VISTA                         080453             244,18
                         VERO CARTAO CREDITO                               126719             533,66
                         VERO CARTAO CREDITO                               126720              95,94
                         VERO CARTAO CREDITO                               126721              47,87
                         CIELO S/A CARTAO CREDITO                          118299              46,64
                         CIELO S/A CARTAO CREDITO                          118300             137,53
                         REDECARD S.A CARTAO CREDITO                       149953              27,27
                         VERO CARTAO DEBITO                                184851           1.325,03
                         VERO CARTAO DEBITO                                184852             511,96
                         CIELO S/A CARTAO DEBITO                           171809             437,77
                     20  VERO BANRICOMPRAS A PRAZO                         044755             415,29
                         VERO BANRICOMPRAS A VISTA                         349028             493,91
                         VERO CARTAO CREDITO                               403626             673,75
                         VERO CARTAO CREDITO                               403627             342,59
                         VERO CARTAO CREDITO                               403628              58,28
                         REDECARD S.A CARTAO CREDITO                       433775              67,90
                         REDECARD S.A CARTAO CREDITO                       433776              67,61
                         VERO CARTAO DEBITO                                472002             297,51
                         VERO CARTAO DEBITO                                472003             131,56
                         RESGATE AUTOMATICO                                000000           9.700,00
                     21  VERO BANRICOMPRAS A PRAZO                         319063             305,54
                         VERO BANRICOMPRAS A VISTA                         679182             473,10
                         VERO CARTAO CREDITO                               724092             594,46
                         VERO CARTAO CREDITO                               724093             290,86
                         CIELO S/A CARTAO CREDITO                          716700             133,04
                         CIELO S/A CARTAO CREDITO                          716701             244,63
                         REDECARD S.A CARTAO CREDITO                       744273             109,34
                         VERO CARTAO DEBITO                                779495             461,80
                         VERO CARTAO DEBITO                                779496              61,27
                         CIELO S/A CARTAO DEBITO                           765528           1.369,41
                     22  VERO BANRICOMPRAS A PRAZO                         625121             272,86
                         VERO BANRICOMPRAS A VISTA                         947266             173,69
                         VERO CARTAO CREDITO                               049605             794,90
                         VERO CARTAO CREDITO                               049606             181,81
                         VERO CARTAO CREDITO                               049607             123,49
                         CIELO S/A CARTAO CREDITO                          040363             115,22
                         CIELO S/A CARTAO CREDITO                          040364              21,09
                         CIELO S/A CARTAO CREDITO                          040365             116,42
                         REDECARD S.A CARTAO CREDITO                       079047             156,19
                         VERO CARTAO DEBITO                                114585             404,29
                         VERO CARTAO DEBITO                                114586             199,87
                         CIELO S/A CARTAO DEBITO                           101926             262,24
                     25  VERO BANRICOMPRAS A PRAZO                         898216             619,60
                         VERO BANRICOMPRAS A VISTA                         263855             333,14
                         VERO CARTAO CREDITO                               656240           2.182,05
                         VERO CARTAO CREDITO                               656241           1.153,70
                         CIELO S/A CARTAO CREDITO                          641077              41,86
                         CIELO S/A CARTAO CREDITO                          641078              93,03
                         REDECARD S.A CARTAO CREDITO                       705335             156,27
                         VERO CARTAO DEBITO                                761345           1.229,09

                         VERO CARTAO DEBITO                                761346             307,53
                         CIELO S/A CARTAO DEBITO                           737468             290,03
                         CIELO S/A CARTAO DEBITO                           737469             412,93
                         RESGATE AUTOMATICO                                000000          74.100,00
                     27  VERO BANRICOMPRAS A PRAZO                         916537             254,76
                         VERO BANRICOMPRAS A VISTA                         463886             910,60
                         VERO CARTAO CREDITO                               027887             801,29
                         VERO CARTAO CREDITO                               027888             479,59
                         VERO CARTAO CREDITO                               027889              41,91
                         VERO CARTAO CREDITO                               224868             267,26
                         VERO CARTAO CREDITO                               224869             161,01
                         VERO CARTAO CREDITO                               224870             144,73
                         CIELO S/A CARTAO CREDITO                          019299              42,74
                         CIELO S/A CARTAO CREDITO                          019300              72,49
                         CIELO S/A CARTAO CREDITO                          217578             141,70
                         REDECARD S.A CARTAO CREDITO                       055906              22,87
                         REDECARD S.A CARTAO CREDITO                       244646              49,75
                         VERO CARTAO DEBITO                                279344             458,92
                         VERO CARTAO DEBITO                                279345             393,74
                         CIELO S/A CARTAO DEBITO                           266081             380,41
                         CIELO S/A CARTAO DEBITO                           266082             848,21
                     28  VERO BANRICOMPRAS A PRAZO                         427354             332,90
                         VERO BANRICOMPRAS A VISTA                         269379             232,29
                         VERO CARTAO CREDITO                               314994             912,53
                         VERO CARTAO CREDITO                               314995             536,39
                         CIELO S/A CARTAO CREDITO                          305504              33,87
                         CIELO S/A CARTAO CREDITO                          305505              56,26
                         REDECARD S.A CARTAO CREDITO                       344054              89,10
                         VERO CARTAO DEBITO                                381678             810,07
                         VERO CARTAO DEBITO                                381679             463,54
                         CIELO S/A CARTAO DEBITO                           367679             285,99
                     29  VERO BANRICOMPRAS A PRAZO                         228273             139,72
                         VERO BANRICOMPRAS A VISTA                         637529             499,77
                         VERO CARTAO CREDITO                               678028             831,50
                         VERO CARTAO CREDITO                               678029             318,86
                         CIELO S/A CARTAO CREDITO                          668143              38,70
                         REDECARD S.A CARTAO CREDITO                       708785              29,82
                         VERO CARTAO DEBITO                                746619           1.159,92
                         CIELO S/A CARTAO DEBITO                           732531             839,64

一项内容的结果是:RESGATE AUTOMATICO 12.400,00
但实际上正确的输出是:RESGATE AUTOMATICO 112.400,00
它缺少第一个数字。

这个脚本对于我的了解水平来说太复杂了,但是在尝试阅读它之后,我猜问题出在脚本的这一部分:

if not "%DataName:~2,1%" == " " goto CheckName
if "%DataName:~0,1%" == ";" goto CheckName
for /F "delims=01234567890" %%A in ("%DataName:~0,2%") do goto CheckName
set "DataName=%DataName:~3%"

根据我的理解(可能是错误的,正如我之前所说的)000000中间一列让脚本理解删除第一个字符(12.400 而不是 112.400)。

有人可以更好地解释我以了解如何解决它吗?


这是修复这两个问题的增强批处理脚本。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DataCount=0"
set "MaxNameLength=19"
set "TempFile1=%TEMP%\Output.tmp"
set "TempFile2=%TEMP%\Sorted.tmp"
set "OutputFile=Output.txt"

"%ProgramFiles(x86)%\CoolUtils\Total PDF Converter\PDFConverter.exe" "%UserProfile%\Desktop\teste.oxps" "%UserProfile%\Desktop\teste.txt"
del /F "%UserProfile%\Desktop\teste.pdf"
%SystemRoot%\System32\findstr.exe /V /R /C:"http" /C:"Banrisul" /C:"Sac" /C:":" /C:"-" /C:"SAC" /C:"OUVIDORIA" /C:"B A N R I S U L" /C:"+" "%UserProfile%\Desktop\teste.txt" >"%TempFile1%"
if not exist "%TempFile1%" goto EndBatch

rem Process each line in the output file line by line with leading
rem spaces and tabs already removed by FOR, but no other characters.
for /F usebackq^ tokens^=*^ eol^= %%I in ("%TempFile1%") do (
    set "DataRow=%%~I"
    call :ProcessLine
)
goto FormatOutput


:ProcessLine
rem Remove all double quotes within the data row.
set "DataRow=%DataRow:"=%"
rem Remove all exclamation marks within the data row.
set "DataRow=%DataRow:!=%"
rem Remove all horizontal tabs within the data row by spaces.
set "DataRow=%DataRow:  = %"
rem Replace all $ by the string #DollarSign# within the data row.
set "DataRow=%DataRow:$=#DollarSign#%"
rem Replace two spaces in series by a dollar sign.
set "DataRow=%DataRow:  =$%"
rem Replace all occurrences of dollar sign plus space by dollar sign.
set "DataRow=%DataRow:$ =$%"

rem Split up the data row using dollar sign as delimiter which can result
rem in four or just three tokenized substrings depending on existence of
rem 01, 02, ... in first data column. Of interest is the name string in
rem first or second data column and the value string in third or fourth
rem data column.
for /F tokens^=1-4^ delims^=$^ eol^= %%J in ("%DataRow%") do (
    if "%%M" == "" (
        set "DataName=%%~J"
        set "DataValue=%%L"
    ) else (
        set "DataName=%%~K"
        set "DataValue=%%M"
    )
)

rem It is safe now to replace the string #DollarSign# back to dollar sign.
set "DataName=%DataName:#DollarSign#=$%"

rem If the first 2 characters of data name are two digits and third
rem character is a space then remove those 3 characters from data name.
if not "%DataName:~2,1%" == " " goto CheckName
if "%DataName:~0,1%" == ";" goto CheckName
for /F "delims=01234567890" %%J in ("%DataName:~0,2%") do goto CheckName
set "DataName=%DataName:~3%"

rem Data names not containing one of the following four strings
rem should be at end of the list and not included in the total sum.
:CheckName
if not "%DataName:DEBITO=%"  == "%DataName%" goto ReformatValue
if not "%DataName:CREDITO=%" == "%DataName%" goto ReformatValue
if not "%DataName:A VISTA=%" == "%DataName%" goto ReformatValue
if not "%DataName:A PRAZO=%" == "%DataName%" goto ReformatValue
set "DataName=z_%DataName%"

rem Floating point arithmetic is not supported by Windows command processor,
rem just signed 32-bit integer arithmetic with values in range -2147483648
rem to 2147483647. Therefore remove all dots and commas from data value.
:ReformatValue
set "DataValue=%DataValue:.=%"
set "DataValue=%DataValue:,=%"
for /F "tokens=* delims=0" %%J in ("%DataValue%") do set "DataValue=%%J"
if not defined DataValue set "DataValue=0"

rem Add the data value as integer to total sum. There is no special
rem overflow handling implemented yet in case of total sum exceeds
rem the maximum 32-bit positive signed integer value 2147483647.
if not "%DataName:~0,2%" == "z_" set /A "#TotalSum+=DataValue"

rem Find out if a data value of current data name is already in list
rem of environment variables and in this case just add the data value
rem to the existing sum for data data name and exit the subroutine.
for /F "tokens=1* delims==" %%J in ('set $\ 2^>nul') do if /I "%%K" == "%DataName%" set /A "#%%~nJ+=DataValue" & goto :EOF

rem This is a new data name with its first data value. So set
rem the appropriate environment variables for name and value.
set /A DataCount+=1
set "$\%DataCount%=%DataName%"
set "#%DataCount%=%DataValue%"

rem For a later aligned output find out the length of the data name and
rem remember its length if being greater than longest data name up to now.
rem z_ added at beginning of some data names must be always ignored for
rem length of data name.
set "NameLength=1"
:GetNameLength
set "DataName=%DataName:~1%"
if not "%DataName%" == "" set /A "NameLength+=1" & goto GetNameLength
if "%DataName:~0,2%" == "z_" set /A NameLength-=2
if %NameLength% GTR %MaxNameLength% set "MaxNameLength=%NameLength%"

rem Exit the subroutine.
goto :EOF


:FormatOutput
rem Add 3 to maximum name length to have always at least 3 spaces
rem between longest data name and the sum of the data values.
set /A MaxNameLength+=3

setlocal EnableDelayedExpansion
rem Build a string consisting of spaces according to maximum name length.
set "SpacesName="
for /L %%I in (1,1,%MaxNameLength%) do set "SpacesName=!SpacesName! "

rem Find out the maximum value which is usually the total sum, but
rem could be also a different value like sum of RESGATE AUTOMATICO.
set "MaximumValue=%#TotalSum%"
setlocal EnableDelayedExpansion
for /F "tokens=2 delims==" %%I in ('set #') do if %%I GTR !MaximumValue! set "MaximumValue=%%I"
endlocal & set "MaximumValue=%MaximumValue%"

rem Format value of maximum value.
call :FormatNumber %MaximumValue%

rem For a later aligned output find out the length of the maximum value.
set "ValueLength=1"
:GetValueLength
set "DataValue=%DataValue:~1%"
if not "%DataValue%" == "" set /A "ValueLength+=1" & goto GetValueLength

rem Build a string consisting of spaces according to maximum name length.
set "SpacesValue="
for /L %%I in (1,1,%ValueLength%) do set "SpacesValue=!SpacesValue! "

rem Output the data names with appropriate number of aligning spaces
rem and the data sum for each data name formatted and with aligning
rem spaces into the output file.
set /A ExtraNameLength=MaxNameLength+2
del "%TempFile1%" 2>nul
(for /F "tokens=1* delims==" %%I in ('set $\ 2^>nul') do (
    set "DataName=%%J%SpacesName%"
    if not "!DataName:~0,2!" == "z_" (
        set "DataName=!DataName:~0,%MaxNameLength%!"
    ) else set "DataName=!DataName:~0,%ExtraNameLength%!"
    call :FormatNumber !#%%~nI!
    set "DataValue=%SpacesValue%!DataValue!"
    set "DataValue=!DataValue:~-%ValueLength%!"
    echo !DataName!!DataValue!
))>>"%TempFile1%"

rem Sort the lines in output file according to name.
%SystemRoot%\System32\sort.exe "%TempFile1%" /O "%TempFile2%"
del "%TempFile1%" 2>nul

rem 16 is the length of string CARDS OF MONTH with a space on both sides.
set /A LineLength=MaxNameLength+ValueLength
set /A HeadLength=(LineLength-16) / 2

rem Build the heading for the output file with centered CARDS OF MONTH
rem and create the output file with this heading as first line.
set "HyphensHead="
for /L %%I in (1,1,%HeadLength%) do set "HyphensHead=!HyphensHead!-"
set "Heading=%HyphensHead% CARDS OF MONTH %HyphensHead%"
set /A HeadLength=HeadLength*2 + 16
if not %HeadLength% == %LineLength% set "Heading=%Heading%-"
echo %Heading%>"%OutputFile%"

set "TotalSumWrite=1"
for /F usebackq^ tokens^=*^ eol^= %%I in ("%TempFile2%") do (
    set "DataRow=%%I"
    if defined TotalSumWrite (
        if not "!DataRow:~0,2!" == "z_" (
            echo !DataRow!>>"%OutputFile%"
        ) else (
            rem Append the total summary to the output file.
            set "TotalSumWrite="
            >>"%OutputFile%" echo %SpacesName: =-%%SpacesValue: =-%
            set "DataName=TOTAL OF ALL:%SpacesName%"
            set "DataName=!DataName:~0,%MaxNameLength%!"
            call :FormatNumber %#TotalSum%
            set "DataValue=%SpacesValue%!DataValue!"
            set "DataValue=!DataValue:~-%ValueLength%!"
            >>"%OutputFile%" echo !DataName!!DataValue!
            >>"%OutputFile%" echo %SpacesName: =-%%SpacesValue: =-%
            >>"%OutputFile%" echo !DataRow:~2!
        )
    ) else echo !DataRow:~2!>>"%OutputFile%"
)

if defined TotalSumWrite (
    >>"%OutputFile%" echo %SpacesName: =-%%SpacesValue: =-%
    set "DataName=TOTAL OF ALL:%SpacesName%"
    set "DataName=!DataName:~0,%MaxNameLength%!"
    call :FormatNumber %#TotalSum%
    set "DataValue=%SpacesValue%!DataValue!"
    set "DataValue=!DataValue:~-%ValueLength%!"
    >>"%OutputFile%" echo !DataName!!DataValue!
)

del "%TempFile2%" 2>nul
endlocal
goto EndBatch


rem The subroutine below reformats 0 to 99 to 0,00 to 0,99 and inserts
rem dots on larger values after a series of 3 digits left to the comma.

:FormatNumber
set "DataValue=%1"
if "%DataValue:~1,1%" == "" set "DataValue=0%DataValue%"
if "%DataValue:~2,1%" == "" set "DataValue=0%DataValue%"
set "DataValue=%DataValue:~0,-2%,%DataValue:~-2%"
if not "%DataValue:~6,1%" == "" set "DataValue=%DataValue:~0,-6%.%DataValue:~-6%"
if not "%DataValue:~10,1%" == "" set "DataValue=%DataValue:~0,-10%.%DataValue:~-10%"
goto :EOF

:EndBatch
endlocal

插入以下两行以修复范围内值的第一个问题0,08 to 0,99由于前导,删除逗号后被错误解释为无效或有效的八进制数而不是十进制数0.

for /F "tokens=* delims=0" %%J in ("%DataValue%") do set "DataValue=%%J"
if not defined DataValue set "DataValue=0"

范围内的所有值0,08 to 0,99带数字8 or 9in value 被解释为无效的八进制数,导致使用 value0在算术表达式中计算总和。

这两行删除所有前导0来自数据值,并且如果由于值已在循环后不再定义数据值0,00、环境变量DataValue被重新定义为值0.

我也将此修复应用于发布在的代码如何使用批处理文件对文本进行排序和求和时忽略第一和第三数据列? https://stackoverflow.com/a/52237641/3074564

输出总和错误的第二个问题RESGATE AUTOMATICO是由以下原因引起的:

rem Format value of total sum.
call :FormatNumber %#TotalSum%

rem For a later aligned output find out the length of the total
rem sum which is expected to be the greatest value in output.

所提供的数据示例无法满足总和始终为输出中的最大值的期望,因为总和为98.847,01同时总和RESGATE AUTOMATICO is 112.400,00其中有更多字符。

此问题已通过以下方式修复:

rem Find out the maximum value which is usually the total sum, but
rem could be also a different value like sum of RESGATE AUTOMATICO.
set "MaximumValue=%#TotalSum%"
setlocal EnableDelayedExpansion
for /F "tokens=2 delims==" %%I in ('set #') do if %%I GTR !MaximumValue! set "MaximumValue=%%I"
endlocal & set "MaximumValue=%MaximumValue%"

rem Format value of maximum value.
call :FormatNumber %MaximumValue%

rem For a later aligned output find out the length of the maximum value.

还有一些其他不影响输出的小改进。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用批处理脚本连接相同名称的内容并将每个内容的值相加 的相关文章

随机推荐